Hello, i’m having a little problem, I have a personal third person character controller, made with multiple tutorials. I want it to be like the ones on Zelda or Super Mario, where de camera follows the player and the player rotates around the camera.
All of that is already done but I came up with a problem in the movement of the player, it moves at where the camera is looking at, which is good but when the camera looks up, the player starts going up like flying, it also goes slower when the camera looks down.
By experimentation its clearly the code of movement, so my question is, how do I impede the player from going up or down when the camera looks up and down?
Here’s the code:
void FixedUpdate()
{
CheckGrounded(); //CHeck if player is on ground or not.
//Inputs
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
jumpInput = Input.GetButtonDown("Jump");
/*
This if statment is how tolerant we are on changing the direction based on where the camera is looking.
For example, if the player is moving to the left/right of where the camera is looking and then he rotates the camera
so it looks towards where he is going, we will keep moving at the same direction as before
*/
storDir = cam.right; //This means, the player can keep moving in the same direction they were before even if they change the camera angle
anim.SetBool("OnAir", onGround);
if (Input.GetKey(KeyCode.LeftAlt))
{
speed = 0.3f;
}
else
speed = 0.8f;
rigidBody.AddForce((storDir * horizontal) + (cam.forward * vertical) * speed / Time.deltaTime);
if (onGround) //Jump!, does not rotate
{
//Physics.gravity = Vector3.down * 9.8f;
//Jump controls
if (jumpInput && onGround)
{
anim.SetTrigger("Jump");
rigidBody.velocity = new Vector3(rigidBody.velocity.x, 1 * jumpPower, rigidBody.velocity.z); //ForceMode.Impulse (I think) gives all the force to the jump for only one frame.
}
}
else
{
rigidBody.AddForce(Vector3.down * extrGravity, ForceMode.Force);
//Physics.gravity = Vector3.down * extrGravity;
}
/*Rotates the Character*/
//Find a position in front of where the camera is looking
directionPos = transform.position + (storDir * horizontal) + (cam.forward * vertical);
//Find the direction from that position
Vector3 dir = directionPos - transform.position;
Vector3 dir2 = - directionPos + transform.position; //Evade rotation when walking bacwards
dir2.y = 0;
dir.y = 0; //The player should not be bouncing up and down.
// Turn the input animator values, since our blend three is directoinal and can only go up to 1
float animvalue2 = horizontal;
float animValue = vertical;
if (Input.GetKey(KeyCode.LeftAlt))
{
animValue *= 0.5f;
animvalue2 *= 0.5f;
}
else
{
animValue = vertical;
animvalue2 = horizontal;
}
// Pass the value of the animator
anim.SetFloat("Forward", animValue, .1f, Time.deltaTime);
anim.SetFloat("Rotate", animvalue2, .1f, Time.deltaTime);
//If the player has been given input, we move!
if (horizontal != 0 || vertical != 0)
{
//find the angle, between the character's rotation and where the camera is looking
float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(dir));
if (vertical < 0)
rigidBody.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir2), turnSpeed * Time.deltaTime);
else
// if angle it's not zero (to avoid a warning)
if (angle != 0) //look towards the camera
{
rigidBody.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), turnSpeed * Time.deltaTime);
}
}
}