- Home /
Clamp horizontal rotation based on current direction
Hi!
So I'm a bit stuck atm.
I'm adding in a vault function into my game and currently that works fine! You move up to the vaultable object, press the "vault" button and it starts the animation for it and after it's finished the player object "teleports" to the position the camera is. No problem there!
The problem is that while the animation plays the player can move the camera around freely which can result in weird 360 spins on the vaultable object as well as problems with the "teleporting" of the player.
Now, I don't want to completly stop the player from being able to move the camera around as that would make the game feel much more locked up and clunky (I know, because I tried). What I want to do is just simply try and clamp the rotation of the player once the vault is initiated so that he/she can only turn the camera ~30 degrees horizontaly towards each side until the animation is finished, with the angle at which the vault was initiated from to be the "origin angle".
Any idea how I could do this in the easiest way possible?
Any help is appreciated!
Code for those who might want to see:
// Camera Movement (Inside Update)
if(mouse_Movement){
//Vertical and Horizontal acceleration
rot_horizontal = Input.GetAxis("Mouse X") * (cam_sensetivity_x / 100);
rot_vertical = -Input.GetAxis("Mouse Y") * (cam_sensetivity_y / 100);
//Applying Rotation
transform.Rotate(0, rot_horizontal, 0);
cam_main.gameObject.transform.Rotate(rot_vertical, 0, 0);
//Calculating vertical rotation to a variable usable with mathf.clamp
float angleX = cam_main.gameObject.transform.localEulerAngles.x;
angleX = (angleX > 180) ? angleX - 360 : angleX;
//Restricting the cameras vertical rotation using mathf.clamp'
cam_main.gameObject.transform.localEulerAngles = new Vector3( Mathf.Clamp( angleX, -60, 60), 0, 0);
} // ===============
{//Vault
//Check if vault key is pressed and if facing the correct way
if(pc_vault && !pc_perfAnim && Input.GetButtonDown("Vault") && Vector3.Angle(transform.forward, trg_vault.transform.position - transform.position) < 70){
StartCoroutine(PlayAnim("vault"));
}
}
//Function that plays the right animation and "teleports" the player
IEnumerator PlayAnim (string anim_type) {
//Disable player movement while animation is playing
TogglePCMovement();
pc_perfAnim = true;
//Performe the different types of Vault animations
if(anim_type == "vault"){
//Get animation name and start animation
string anim_name = trg_vault.tag;
anim_ctr.SetBool(anim_name, true);
//Wait for animation to finish playing...
yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(anim_ctr.GetCurrentAnimatorStateInfo(0).length);
//Performe vault repositioning for each vault type
if(anim_name == "Vault"){
transform.position += transform.up *1f;
transform.position += transform.forward * 1f;
} else if(anim_name == "Vault Over"){
transform.position += transform.forward * 1.5f;
} else if(anim_name == "Climb"){
transform.position += transform.up *2f;
transform.position += transform.forward * 1f;
} else if(anim_name == "Climb Over"){
transform.position += transform.up *1f;
transform.position += transform.forward * 1f;
}
//Stop playing animation, return to normal animation state
anim_ctr.SetBool(anim_name, false);
}
//Enable player movement again
pc_perfAnim = false;
TogglePCMovement();
//Final return for the sake of not getting errors :P
yield return new WaitForEndOfFrame();
}
Your answer
Follow this Question
Related Questions
I can't figure out how to clamp it 2 Answers
How to Math.f Clamp this 0 Answers
Camera Clamping Third Person? 0 Answers
Problems with turret rotation clamp 1 Answer
Clamp issue 1 Answer