I’m new using mecanim and i find myself in a problem, in my game (TopDown view) i can rotate the character towards the mouse position, but i wanna make this only in some animations states, how i can do this?
I achieve rotation only in the ground and remove rotation when i have a key pressed but i wanna remove it in a animation state.
Here is my code: (I try some things to archive the rotations what i wanna and those are commented out)
public class TranscendenceController : MonoBehaviour {
private Animator anim;
private AnimatorStateInfo currentBaseState;
private bool grounded;
public float animSpeed = 1f;
public float MouseRotspeed = 2f;
public float gravityMultiplier = 10;
public float setFloatDampTime = 0.17f;
void Start () {
anim = GetComponent<Animator>();
}
void Update() {
Screen.showCursor = true;
Screen.lockCursor = false;
anim.applyRootMotion = grounded;
}
void FixedUpdate () {
float Vertical = Input.GetAxis("Vertical");
float MouseHorizontal = Input.GetAxis("Mouse X");
bool Slide = Input.GetKey (KeyCode.C);
bool Sprint = Input.GetKey (KeyCode.LeftShift);
bool Walk = Input.GetKey (KeyCode.LeftAlt);
bool Jump = Input.GetKey (KeyCode.Space);
anim.SetBool ("Slide", Slide);
anim.SetBool ("Sprint", Sprint);
anim.SetBool ("Walk", Walk);
anim.SetBool ("Jump", Jump);
anim.SetFloat("Vertical", Vertical);
anim.SetFloat("MouseHorizontal", MouseHorizontal, setFloatDampTime * 4, Time.deltaTime);
rigidbody.useGravity = true;
Vector3 extraGravityForce = (Physics.gravity * gravityMultiplier) - Physics.gravity;
rigidbody.AddForce(extraGravityForce);
//HERE START THE CODE FOR ROTATION
Vector3 playerPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
Plane playPlane = new Plane(Vector3.up, transform.position);
Ray mouseray = Camera.main.ScreenPointToRay(playerPos);
float mousehitDistance = 0.0f;
if (grounded) { //HERE ONLY ROTATES WHEN IM GROUNDED AND NOT IN THE AIR
if (playPlane.Raycast(mouseray,out mousehitDistance)) {
Vector3 targetPoint = mouseray.GetPoint(mousehitDistance);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint-transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation, MouseRotspeed*Time.deltaTime);
}
}
// I tried this and works to remove the rotation in some animations but only when i have pressed the key
// And not in the complete AnimState
//if (Walk){
// MouseRotspeed = 0;
//}
//else{
// MouseRotspeed = 2;
//}
}
void OnTriggerEnter(Collider other){
if(other.tag != "Enemy"){
grounded = true;
}
}
void OnTriggerStay(Collider other){
if(other.tag != "Enemy"){
grounded = true;
}
}
void OnTriggerExit(Collider other){
if(other.tag != "Enemy"){
grounded = false;
}
}
}
So that is what i have until now for some reason the grounded not count like in air when i jump but works well when i fall from a place.
What i need is find a way to remove the mouse rotation in some Animations States like the slide or a jump…
So if you know a way to do this or know any example please help me!
Sorry for my English is not my main language