Search Unity

Question Player not moving horizontally

Discussion in '2D' started by TheSnowyDev, Sep 30, 2020.

  1. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    So I'm trying to get my player character to move both vertically and horizontally, but while the vertical movement is working fine, the player is constantly stuck at 0 on the x axis. Now, I did change the Horizontal axis to only use the a and d keys, so I'm wondering if that tampering could have fooled things up, but IDK. Any thoughts?

    Here's the code, and my altered input for the Horizontal axis is attached below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float turnSpeed = 250f;
    8.     public float moveSpeed = 500f;
    9.  
    10.     GameObject playerCam;
    11.  
    12.     Rigidbody2D playerRb;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         playerCam = GameObject.Find("Main Camera");
    18.  
    19.         playerRb = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         playerCam.transform.position = new Vector3(transform.position.x, transform.position.y, -10);
    26.     }
    27.  
    28.     private void FixedUpdate()
    29.     {
    30.         Move();
    31.     }
    32.  
    33.     void Move()
    34.     {
    35.         float vertical = Input.GetAxis("Vertical") * moveSpeed;
    36.         float horizontal = Input.GetAxis("Horizontal") * moveSpeed;
    37.         float turn = Input.GetAxis("Look") * turnSpeed * Time.deltaTime;
    38.  
    39.         playerRb.MovePosition(playerRb.position + Vector2.up * vertical * Time.deltaTime);
    40.         //No horizontal movement
    41.         playerRb.MovePosition(playerRb.position + Vector2.right * horizontal * Time.deltaTime);
    42.  
    43.         //Player rotation
    44.         transform.Rotate(Vector3.forward * turn);
    45.     }
    46. }
    47.  
    Capture.PNG
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Hello @CloakedDaggerr,
    If you print out the value of
    horizontal
    , does it change when you press the keys assigned to horizontal movement?

    Do you have any collider/gravity to counter act the translation?
     
  3. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    Horizontal translation is working now, but vertical position is stuck at 0Y. I threw in a Debug.Log to check the input, and the float values are changing as normal corresponding to the inputs.

    I have no colliders or gravity in the project, so I don't know what the problem is. Any thoughts?
     
  4. adt107118

    adt107118

    Joined:
    Jul 2, 2019
    Posts:
    3
    do u have that file to us to check what's the problem ?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    There's lots of problems here. First, don't modify the Transform, that's the responsibility of the Rigidbody2D. It has no way of controlling the Transform correctly if you keep modifying it. If you add a Rigidbody2D then you should always go through it's API to get it to move. Anything else is bypassing it and is undermining what it's trying to do. You're using MovePosition already so if you want it to rotate then use MoveRotation.

    Issuing a MovePosition will be actioned during the next simulation step. If you issue another one before that then that one alone will be used. These are not actioned immediately and are not queued as commands. You should calculate the position you want to move to then issue a single move.

    Note that whilst MovePosition will work for Dynamic bodies, it's designed for Kinematic bodies as those typically use absolute user-defined position/rotations. Doing this on a Dynamic body will work but you also bypass any in-built velocities it might have as well as how gravity is applied. If you're doing kinematic movement like this then you should add in the gravity term yourself as part of the single position you wish to move to.

    Final note: You scale by Time.deltaTime however that is (technically) incorrect as that is the per-frame time. You should use Time.fixedDeltaTime. The reason why it works though is because many years ago, Unity decided that if a user reads Time.deltaTime during the script fixed-update callbacks, it will return Time.fixedDeltaTime instead as that's probably the intent. Whist it will work, know that the two are different and if you use that outside the fixed-update you'll actually get the per-frame (render) time.

    Hope that helps.
     
    TheSnowyDev likes this.