Search Unity

Y axis is changing randomly

Discussion in 'Scripting' started by NintendoMaster00, Jul 31, 2015.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I am making a 2d platformer type game and on the player controller script (it is like the one from the live training)
    whenever I walk the y axis kind of bounces, it goes up by about 0.3 to 0.4 every time the an arrow key is pressed:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.      
    6.         [HideInInspector] public bool facingRight = true;
    7.         [HideInInspector] public bool jump = false;
    8.         public float moveForce = 200f;
    9.         public float maxSpeed = 1f;
    10.         public float jumpForce = 500f;
    11.         public Transform groundCheck;
    12.  
    13.  
    14.  
    15.         private float lockPos = 0;
    16.         private bool grounded = false;
    17.         private Animator anim;
    18.         private Rigidbody2D rb2d;
    19.      
    20.      
    21.         // Use this for initialization
    22.         void Awake ()
    23.         {
    24.             anim = GetComponent<Animator>();
    25.             rb2d = GetComponent<Rigidbody2D>();
    26.         }
    27.      
    28.         // Update is called once per frame
    29.         void Update ()
    30.         {
    31.             //locks position values
    32.             transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.z, lockPos, lockPos);
    33.  
    34.             grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("ground"));
    35.          
    36.             if (Input.GetButtonDown("Jump") && grounded)
    37.             {
    38.                 jump = true;
    39.             }
    40.         }
    41.      
    42.         void FixedUpdate()
    43.         {
    44.             float h = Input.GetAxis("Horizontal");
    45.          
    46.             anim.SetFloat("Speed", Mathf.Abs(h));
    47.          
    48.             if (h * rb2d.velocity.x < maxSpeed)
    49.                 rb2d.AddForce(Vector2.right * h * moveForce);
    50.          
    51.             if (Mathf.Abs (rb2d.velocity.x) > maxSpeed)
    52.                 rb2d.velocity = new Vector2(Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
    53.          
    54.             if (h > 0 && !facingRight)
    55.                 Flip ();
    56.             else if (h < 0 && facingRight)
    57.                 Flip ();
    58.          
    59.             if (jump)
    60.             {
    61.                 anim.SetTrigger("Jump");
    62.                 rb2d.AddForce(new Vector2(0f, jumpForce));
    63.                 jump = false;
    64.             }
    65.         }
    66.      
    67.      
    68.         void Flip()
    69.         {
    70.             facingRight = !facingRight;
    71.             Vector3 theScale = transform.localScale;
    72.             theScale.x *= -1;
    73.             transform.localScale = theScale;
    74.         }
    75.     }
    also the z rotation would go to 90 whenever i walked so I put in
    transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.z, lockPos, lockPos);
    and that has stopped that problem but I would like to know what caused it in the first place,

    Thanks
     
    Last edited: Jul 31, 2015
  2. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86