Jump to content
Unity Insider Forum

Android Controller in Script


Recommended Posts

Hallo, ich kenn mich hier noch nicht aus weil ich mich gerade registriert habe und jetzt einfach ein neues Thema machen, ich kenn mich mit Android development noch nicht so aus und brauche Hilfe, wie kann ich mit Buttons für Android den Player Steuern? Also bei einem Jump button springt der Player und so. Bei dem Pause Menü usw geht dass schon von Haus aus. Danke

Hier das Player Script:

 

using UnityEngine;

using System.Collections;

 

public class Player : MonoBehaviour {

 

//Floats

public float maxSpeed = 3;

public float speed = 50f;

public float jumpPower = 150f;

 

//Booleans

public bool grounded;

public bool canDoubleJump;

 

//Stats

public int curHealth;

public int maxHealth = 3;

 

//References

private Rigidbody2D rb2d;

private Animator anim;

 

 

 

void Start ()

{

rb2d = gameObject.GetComponent<Rigidbody2D>();

anim = gameObject.GetComponent<Animator>();

 

curHealth = maxHealth;

 

}

 

 

void Update ()

{

 

 

anim.SetBool("Grounded",grounded);

anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));

 

if (Input.GetAxis("Horizontal") < -0.1f)

{

transform.localScale = new Vector3(-1, 1, 1);

}

 

if (Input.GetAxis("Horizontal") > 0.1f)

{

transform.localScale = new Vector3(1, 1, 1);

}

 

if (Input.GetButtonDown("Jump"))

{

if (grounded && Input.GetButtonDown("Jump"))

{

rb2d.AddForce(Vector2.up * jumpPower);

grounded = false;

}

else

{

 

if (canDoubleJump)

{

 

canDoubleJump = false;

rb2d.velocity = new Vector2(rb2d.velocity.x, 0);

rb2d.AddForce(Vector2.up * jumpPower / 1.75f);

 

}

 

}

}

 

if(curHealth > maxHealth){

curHealth = maxHealth;

}

 

if(curHealth <= 0){

 

curHealth = 0;

 

Die ();

 

}

 

 

 

}

 

void FixedUpdate()

{

{

 

 

}

Vector3 easeVelocity = rb2d.velocity;

easeVelocity.y = rb2d.velocity.y;

easeVelocity.z = 0.0f;

easeVelocity.x *= 0.75f;

 

float h = Input.GetAxis("Horizontal");

 

//Fake friction / Easing the x speed of our player

if (grounded)

{

 

rb2d.velocity = easeVelocity;

 

}

 

 

//Moving the player

rb2d.AddForce((Vector2.right * speed) * h);

 

//Limiting the speed of the player

if (rb2d.velocity.x > maxSpeed)

{

rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);

}

 

if (rb2d.velocity.x < -maxSpeed)

{

rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);

}

 

}

 

void Die(){

 

Application.LoadLevel(Application.loadedLevel);

 

}

 

public void Damage(int dmg){

 

curHealth -= dmg;

gameObject.GetComponent<Animation>().Play("RedFlash1");

 

}

 

 

public IEnumerator KnockBack (float knockDur, float knockBackPwr, Vector3 knockBackDir)

{

 

float timer = 0;

while (knockDur>timer) {

timer += Time.deltaTime;

rb2d.velocity = new Vector2 (0, 0);

rb2d.AddForce (new Vector3 (knockBackDir.x * -10, knockBackDir.y * knockBackPwr, transform.position.z));

 

 

}

yield return 0;

 

}

 

}

Link zu diesem Kommentar
Auf anderen Seiten teilen

Herzlich willkommen im Forum.

 

Das du dich gerade registriert hast: ein Doppelpost mit dem gleichen Inhalt unter zwei verschiedenen Überschriften kommt nicht so gut an.

 

Jetzt zu deiner Frage. Dazu muss ich vorher ein paar Sachen wissen. Das Script oben sieht auf den ersten Blick schon mal ziemlich gut aus. Hast du das selbst erstellt? Ich frage nur, weil der Satz

 

Bei dem Pause Menü usw geht dass schon von Haus aus.

eher darauf hinweist, dass du mit der Materie noch nicht so vertraut bist. Bei einem Unity-Game funktioniert erst mal nichts "von Haus aus", was du nicht selbst gemacht hast. Ich würde solche Automatiken auch nicht haben wollen.

 

Gleich die nächste Frage: Was sind "Buttons für Android"?

 

Wir müssen wissen, welche Grundlagen du mitbringst, sonst hilft dir das, was wir hier schreiben, nicht besonders viel.

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...