Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Character Won't Jump When Space is Pressed

Discussion in '2D' started by Mick_Ash, Oct 10, 2023.

  1. Mick_Ash

    Mick_Ash

    Joined:
    Oct 6, 2023
    Posts:
    28
    Hi all,

    For a class I'm taking, I have to make a platformer, and plan on making my character jump when the spacebar is hit. For some reason, this isn't working.

    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     [SerializeField] private float speed = 2f;
    9.     [SerializeField] private float force = 10f;
    10.    
    11.  
    12.     private void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetKey(KeyCode.RightArrow))
    21.         {
    22.             transform.position += new Vector3(speed * Time.deltaTime, 0f, 0f);
    23.         }
    24.         if (Input.GetKey(KeyCode.LeftArrow))
    25.         {
    26.             transform.position -= new Vector3(speed * Time.deltaTime, 0f, 0f);
    27.  
    28.         }
    29.         if (Input.GetKeyDown(KeyCode.Space))
    30.         {
    31.             rb.AddForce(Vector2.up * force, ForceMode2D.Impulse);
    32.  
    33.         }
    34.     }
    35. }
    Any ideas on what to do?
     
  2. DanielTanBK

    DanielTanBK

    Unity Technologies

    Joined:
    Aug 20, 2019
    Posts:
    83
    Hello, in this case your
    transform.position
    is overriding the
    rb.AddForce

    It is not recommended to modify the transform directly while using a Rigidbody as this will cause issues like unpredictable movement or GameObjects passing through and into each other.

    You can refer to our documentation on how it works here and more info on Dynamic or Kinematic Body Types to suit your gameplay needs
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    You are misusing the physics system, bypassing it with direct transform manipulation. Don't do that.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    This means you may not change transform.position, transform.rotation, you may not call transform.Translate(), transform.Rotate() or other such methods, and also transform.localScale is off limits. You also cannot set rigidbody.position or rigidbody.rotation directly. These ALL bypass physics.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
  4. Mick_Ash

    Mick_Ash

    Joined:
    Oct 6, 2023
    Posts:
    28
    How would I edit the code to include .MovePosition to the RigidBody2D so it works properly? I'm fairly new to Unity, so I'm a bit lost on these subjects.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Definitely start with the API documentation itself, there's probably example code.

    Otherwise tutorials for how to use MovePosition do exist

    If you wanna go at it yourself, break apart line 22 (and line 26) so that it extracts the value, adds the movement, then assigns the value back to transform.

    Finally replace that third step: instead of assigning it to transform, call .MovePosition() on it.

    Imphenzia: How Did I Learn To Make Games: