Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityStandardAssets.CrossPlatformInput;
- public class PlayerPhysics : MonoBehaviour {
- //Erstellte Variablen
- Rigidbody2D rb;
- float dirX;
- public float speed = 5f;
- public float jumpHeight = 600f;
- public float maxSpeed = 3;
- public bool grounded;
- bool facingRight = true;
- Vector3 localScale;
- private Animator anim;
- void Start () {
- localScale = transform.localScale;
- rb = GetComponent<Rigidbody2D> ();
- //Animationen des Char.
- anim = gameObject.GetComponent<Animator>();
- }
- void Update () {
- //Steuerung
- dirX = CrossPlatformInputManager.GetAxis ("Horizontal");
- if (CrossPlatformInputManager.GetButtonDown ("Jump"))
- Jump ();
- //Animationen auf Char. bezug
- anim.SetBool("Grounded", grounded);
- anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
- //Drehen der Texturen während des Movements
- if(CrossPlatformInputManager.GetAxis("Horizontal") < -0.1f)
- {
- transform.localScale = new Vector3(-1, 1, 1);
- }
- if (CrossPlatformInputManager.GetAxis("Horizontal") < 0.1f)
- {
- transform.localScale = new Vector3(1, 1, 1);
- }
- }
- void FixedUpdate()
- {
- //Laufen
- rb.velocity = new Vector2 (dirX * speed, rb.velocity.y);
- //Speed limit
- if (rb.velocity.x > maxSpeed)
- {
- rb.velocity = new Vector2(maxSpeed, rb.velocity.y);
- }
- if (rb.velocity.x < -maxSpeed)
- {
- rb.velocity = new Vector2(-maxSpeed, rb.velocity.y);
- }
- }
- void LateUpdate()
- {
- CheckWhereToFace ();
- }
- void CheckWhereToFace()
- {
- if (dirX > 0)
- facingRight = true;
- else
- if (dirX < 0)
- facingRight = false;
- if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
- localScale.x *= -1;
- transform.localScale = localScale;
- }
- void Jump()
- {
- if (CrossPlatformInputManager.GetButtonDown("Jump") && grounded)
- {
- rb.AddForce(Vector2.up * jumpHeight);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement