So I’m trying to fix my player control script & it’s almost working fine, except for some reason it allows the player to jump fine with the correct gravity, but for some reason, the following line{s} are causing the player to fall more slowly than normal & if continually colliding with something, it just sticks there until the key is let go of then continues to fall more slowly.
Why are these lines
if ( isGrounded == true ) {
// Current movement
rb.velocity = new Vector3 (
moveHorizontal,
0.0f,
moveVertical
).normalized * speed;
}
else {
rb.AddForce (
movementRotation * speed * jumpModifier
);
}
causing my player to fall at a slower rate & stick to the colliding object until the movement key{s} are let go, than if I press the jump button & let the player fall?
Here’s the function :
void ControlPlayer ( ) {
moveHorizontal = Input.GetAxis ( "Horizontal" );
moveVertical = Input.GetAxis ( "Vertical" );
Debug.Log ( direction );
Vector3 camForward = cam.transform.forward;
camForward.y = 0.0f;
Quaternion camRotationFlattened = Quaternion.LookRotation ( camForward );
movementRotation = camRotationFlattened * movementRotation;
if ( isGrounded == true ) {
// Current movement
rb.velocity = new Vector3 (
moveHorizontal,
0.0f,
moveVertical
).normalized * speed;
}
else {
rb.AddForce (
movementRotation * speed * jumpModifier
);
}
if ( Input.GetKey ( KeyCode.Space ) || Input.GetKey ( KeyCode.Joystick1Button1 ) && isGrounded ) {
jumpHeight = ( jumpForce * jumpModifier );
rb.velocity = new Vector3 (
rb.velocity.x,
jumpHeight,
rb.velocity.z
);
isGrounded = false;
}
if ( movementRotation.sqrMagnitude > 0.1f && isGrounded ) {
transform.rotation = Quaternion.Slerp (
transform.rotation,
Quaternion.LookRotation ( movementRotation ),
10.0f
);
}
}
Here’s the whole script :
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour {
[ Header ( "Target{s}" ) ]
public Camera cam;
public Rigidbody rb;
[ Header ( "Physics" ) ]
public float speed;
public float jumpForce = 17.0f;
public float gravity = 45.0f;
public float jumpModifier = 2.0f;
private float jumpHeight = 0.0f;
private float directionSpeed = 3.0f;
private float speedPlus = 0.0f;
private float direction = 0.0f;
[ Header ( "Grounded Check { Unhidden }" ) ]
public bool isGrounded = true;
[ Header ( "Jump Vector3 { Unhidden }" ) ]
public Vector3 jump;
private float moveHorizontal;
private float moveVertical;
private Vector3 movement;
private Vector3 movementRotation;
private Vector3 moveDirection;
private Vector3 curLoc;
private Vector3 prevLoc;
void Awake ( ) {
cam = Camera.main; // GetComponent <Camera> ( )
rb = GetComponent <Rigidbody> ( );
jump = new Vector3 ( 0.0f, 0.0f, 0.0f );
Physics.gravity = new Vector3 (
0.0f,
-gravity,
0.0f
);
}
void OnCollisionStay ( ) {
isGrounded = true;
}
void ControlPlayer ( ) {
moveHorizontal = Input.GetAxis ( "Horizontal" );
moveVertical = Input.GetAxis ( "Vertical" );
Debug.Log ( direction );
Vector3 camForward = cam.transform.forward;
camForward.y = 0.0f;
Quaternion camRotationFlattened = Quaternion.LookRotation ( camForward );
movementRotation = camRotationFlattened * movementRotation;
if ( isGrounded == true ) {
// Current movement
rb.velocity = new Vector3 (
moveHorizontal,
0.0f,
moveVertical
).normalized * speed;
}
else {
rb.AddForce (
movementRotation * speed * jumpModifier
);
}
if ( Input.GetKey ( KeyCode.Space ) || Input.GetKey ( KeyCode.Joystick1Button1 ) && isGrounded ) {
jumpHeight = ( jumpForce * jumpModifier );
rb.velocity = new Vector3 (
rb.velocity.x,
jumpHeight,
rb.velocity.z
);
isGrounded = false;
}
if ( movementRotation.sqrMagnitude > 0.1f && isGrounded ) {
transform.rotation = Quaternion.Slerp (
transform.rotation,
Quaternion.LookRotation ( movementRotation ),
10.0f
);
}
}
public void StickToWorldspace ( Transform root, Transform camera, ref float directionOut, ref float speedOut ) {
Vector3 rootDirection = root.forward;
Vector3 stickDirection = new Vector3 (
moveHorizontal,
0.0f,
moveVertical
);
speedOut = stickDirection.sqrMagnitude;
Vector3 CameraDirection = camera.forward;
CameraDirection.y = 0.0f;
Quaternion referentialShift = Quaternion.FromToRotation (
rootDirection,
Vector3.Normalize ( CameraDirection )
);
moveDirection = referentialShift * stickDirection;
Vector3 axisSign = Vector3.Cross (
moveDirection,
rootDirection
);
// Will always face opposite to the camera
Debug.DrawRay (
new Vector3 (
root.position.x,
root.position.y + 5.0f,
root.position.z
),
moveDirection,
Color.green
);
// Direction the player is facing
Debug.DrawRay (
new Vector3 (
root.position.x,
root.position.y + 5.0f,
root.position.z
),
rootDirection,
Color.magenta
);
// Creates Angle between the two
float angleRootToMove = Vector3.Angle (
rootDirection,
moveDirection
);
directionOut = angleRootToMove;
Debug.Log ( directionOut );
}
void FixedUpdate ( ) {
ControlPlayer ( );
StickToWorldspace (
this.transform,
cam.transform,
ref direction,
ref speedPlus
);
}
}
Any help is most definitely appreciated!
Thank you & have a great evening!