Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple odd movement behaviors with character

Discussion in '2D' started by TreeTree3, Aug 31, 2014.

  1. TreeTree3

    TreeTree3

    Joined:
    Aug 29, 2014
    Posts:
    2
    I've essentially copied the movement code from the 2D tutorial from https://www.assetstore.unity3d.com/en/#!/content/11228

    There are 2 weird behaviors with my code so far. The first one is when running on the ground, the higher my move force, the faster the character runs at even with the same max move speed. I believe I noticed this happening in the 2D tutorial as well. This does not happen when the character is in the air.

    The second weird thing which isn't present in the 2D tutorial is that whenever the character jumps, any horizontal movement will reduce the max height. So if I'm standing still I'll jump to 10 units high, but if I jump while running the entire time I'll only reach about 6 units high. If I jump and run a little bit, I'll reach a little bit lower than 10 units. I don't think I'm modifying the vertical force or velocity in any way except for when I jump, so I don't understand why this is happening.

    Code (csharp):
    1.     public float maxMoveSpeed = 6f;
    2.     public float moveForce = 200f;
    3.    
    4.     private Transform groundCheck;
    5.     private bool grounded = false;
    6.     public float jumpForce = 500f;
    7.     private bool jump = false;
    8.    
    9.     private Animator anim;
    10.    
    11.     void Awake () {
    12.         groundCheck = transform.Find ("groundCheck");
    13.     }
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.         anim = GetComponent <Animator> ();
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));  
    23.        
    24.         if (Input.GetButtonDown ("Jump") && grounded) {
    25.             jump = true;
    26.         }
    27.     }
    28.    
    29.     void FixedUpdate () {
    30.         float h = Input.GetAxis ("Horizontal");
    31.        
    32.         if (h * rigidbody2D.velocity.x < maxMoveSpeed) {
    33.             rigidbody2D.AddForce (Vector2.right * h * moveForce);
    34.         }
    35.        
    36.         if (Mathf.Abs (rigidbody2D.velocity.x) > maxMoveSpeed) {
    37.             rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * maxMoveSpeed, rigidbody2D.velocity.y);
    38.         }
    39.        
    40.         anim.SetFloat ("Axis", Mathf.Abs (h));
    41.        
    42.         if (h > 0) {
    43.             transform.localScale = new Vector3 (1, transform.localScale.y, transform.localScale.z);
    44.         } else if (h < 0) {
    45.             transform.localScale = new Vector3 (-1, transform.localScale.y, transform.localScale.z);
    46.         } else {
    47.             rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x * 0.7f, rigidbody2D.velocity.y);
    48.         }
    49.        
    50.         if (jump) {
    51.             rigidbody2D.AddForce (new Vector2 (0, jumpForce));
    52.             jump = false;
    53.         }
    54.     }
     
  2. TreeTree3

    TreeTree3

    Joined:
    Aug 29, 2014
    Posts:
    2
    After watching more tutorials I discovered both of these behaviors were caused by my floor having a rigidbody. Removing it was the solution.