Issue with smooth movement / jumping?

so I’m trying to move my player object smoothly / jump smoothly, but problem is it is not moving or jumping at all. I need to know how to fix.

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

   // player's moving speed
   public float speed = 0.1f;

   // player's jumping force
   public float jumpforce = 40.0f;


   // whether the player can jump or not
   // true if it's on a surface,
   // false if it's in
   // the air
   private bool CanJump = false;

   public Transform target;
   private Rigidbody rb;

   void OnCollisionEnter ( Collision col ) {
       if  ( col.gameObject.tag == "Ground" ) {
           Debug.Log ( "Hit" + " " + col.gameObject.tag );
           CanJump = true;
       }
   }

   void OnCollisionExit ( Collision col ) {
       if ( col.gameObject.tag == "Ground" ) {
           Debug.Log ( "Exited" + " " + col.gameObject.tag );
           CanJump = false;
       }
   }

   void Start ( ) {
       cam = GetComponent <Camera> ( );
       target = GetComponent <Transform> ( );
       rb = GetComponent <Rigidbody> ( );
   }

   void Update ( ) {
       // Player's Movement. Left side is for moving via
       // the UI, right side is moving
       // via keyboard
       if ( Input.GetKey ( KeyCode.UpArrow ) || Input.GetKey ( KeyCode.W ) ) {
           rb.AddForce (
                   target.forward * speed
           );
       }
       if ( Input.GetKey ( KeyCode.DownArrow ) || Input.GetKey ( KeyCode.S ) ) {
           rb.AddForce (
                   -target.forward * speed
           );
       }
       if ( Input.GetKey ( KeyCode.LeftArrow ) || Input.GetKey ( KeyCode.A ) ) {
           rb.AddForce (
                   -target.right * speed
           );
       }
       if ( Input.GetKey ( KeyCode.RightArrow ) || Input.GetKey ( KeyCode.D ) ) {
           rb.AddForce (
                   target.right * speed
           );
       }
       if ( Input.GetKeyDown ( KeyCode.Space ) && CanJump == false ) {
           CanJump = true;
           Debug.Log ( CanJump );
           rb.AddForce (
                   target.up * jumpforce,
                   ForceMode.Impulse
           );
       }
   }

}

Any help is DEFINITELY appreciated!

Thanks in advance!

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.