Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player doesn't move correctly.

Discussion in 'Scripting' started by Vini310, Jun 15, 2020.

  1. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    I'm creating a runner game and I'm having a very weird problem with the character's movement.
    Code (CSharp):
    1. public class NaomiMovement : MonoBehaviour
    2. {
    3.     public float moveSpeed;
    4.     public float jumpForce;
    5.    
    6.     public Rigidbody myRigidbody;
    7.    
    8.     public bool grounded;
    9.     public LayerMask whatIsGround;
    10.    
    11.     private Collider myCollider;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         myRigidbody = GetComponent<Rigidbody>();
    16.         myCollider = GetComponent<Collider>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         grounded = Physics.IsTouchingLayers(myCollider, whatIsGround);
    23.         myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
    24.        
    25.         if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))
    26.         {
    27.             if(grounded)
    28.             {
    29.                 myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
    30.             }
    31.         }
    32.     }
    33. }
    34.  
    The character was supposed to move to the RIGHT, but it's moving to the LEFT instead, and is going through the ground DESPITE ALL THE LAYERS I added! Selecting "Kinematic" in the rigidbody section hinders the character's movements completely.

    I need to deliver the tech demo by Friday, but I can't continue if the character does not moves properly!
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    try debug.logging your movespeed, make sure its not negative
     
  3. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    I could try to do that, if wasn't for another error in the script ("'Physics' does not contain a definition for 'IsTouchingLayers'").
     
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you probably need to do

    Code (CSharp):
    1. myCollider.IsTouchingLayers(whatIsGround);
    you also need to be careful about using rigidbody2d and collider2d, seems ambiguous if you are using the 2d or 3d physics
     
  5. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    Well, about that:because my game, while using 3D models, still has 2D movement (like, say, Super Mario Run), I used a 2D script as reference, only modifying it to be compatible with a 3D environment.

    Now it's giving a new error:
    Assets\Scripts\NaomiMovement.cs(27,25): error CS1061: 'Collider' does not contain a definition for 'IsTouchingLayers' and no accessible extension method 'IsTouchingLayers' accepting a first argument of type 'Collider' could be found (are you missing a using directive or an assembly reference?)
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    IsTouchingLayers is only valid for 2dcolliders
     
  7. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    I'm using 3D colliders.
     
  8. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    then you cant use istouching
     
  9. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    So I'm gonna just remove the line, since the other problem hasn't been solved (character still walking in the wrong direction).
     
  10. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    well then now you can debug your movespeed

    you either have a negative value or your scene is rotated the wrong way
     
  11. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    It's both. Debug Logging shows the character is going in a negative direction and I also realized my map is reversed (it's still in the X axis, but if the character is going in a negative X axis, it means my map is in the positive one).
     
  12. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    well i guess then you can fix it by rotating everything to 0
     
  13. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    The entire map's rotation is already 0 on all axis, and the character is 0 on X and Z axis, -82.92 on Y axis.
    EDIT: Looks like I fixed it
    Code (CSharp):
    1. public class NaomiMovement : MonoBehaviour
    2. {
    3.     public float moveSpeed;
    4.     public float jumpForce;
    5.    
    6.     public Rigidbody myRigidbody;
    7.    
    8.     public bool grounded;
    9.     public LayerMask whatIsGround;
    10.    
    11.     private Collider myCollider;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         myRigidbody = GetComponent<Rigidbody>();
    16.         myCollider = GetComponent<Collider>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         myRigidbody.velocity = new Vector2(-moveSpeed, myRigidbody.velocity.y);
    23.         if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))
    24.         {
    25.             if(grounded)
    26.             {
    27.                 myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
    28.             }
    29.         }
    30.         Debug.Log(myRigidbody.velocity.y);
    31.         Debug.Log(myRigidbody.velocity.x);
    32.     }
    33. }
     
    Last edited: Jun 16, 2020