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!