- Home /
Quick Question About Player Movement And Animations. Please Help!
I am Just Wondering How I can get My player to play an animation when I rotate him left or right with the mouse?
Answer by AbubakrKhan · Feb 10, 2019 at 03:36 AM
if (Input.GetMouseButton(0)){
anim.Play("animation")
transform.Rotate(new Vector3(0.0f, Input.GetAxis("Mouse X"), 0.0f) * -rotationSpeed);
}
I tried this and It Says -rotationSpeed doesn't exist in this context?
Assign a value to -rotationspeed like
[SerializeField]
private float rotationSpeed = 100f;
void Update() {
if (Input.Get$$anonymous$$ouseButton(0)){
anim.Play("animation");
transform.Rotate(new Vector3(0.0f, Input.GetAxis("$$anonymous$$ouse X"), 0.0f) * Time.deltaTime * -rotationSpeed);
} // now it will work.
}
Thanks, But Will this work for two separate anims I have an anim for left and right rotation
Answer by Crumpet · Feb 10, 2019 at 02:34 AM
can't say I actually checked that this script works but something like this?
public float mousepos = 0;
public Animation anim;
void Start()
{
anim = gameObject.GetComponent<Animation>();
}
void Update()
{
float x = Input.mousePosition.x;
if(x < mousepos) //left
{
anim.Play("asdf1");
}
if(x > mousepos) //right
{
anim.Play("asdf");
}
mousepos = x;
}
It Works But It Loops Need A Way To Stop Playing The Animation If I Am Not Rotating The $$anonymous$$ouse
what are you actually making? like a turning animation for a 3d character? you need to specify that the animation should stop...
if (x == mousepos)
{
anim.Play("Idle");
}
the characters you can import from unity has a prefab that rotates smoothly using the arrow keys if that's what you're looking for just copy that and change it to use mouse movement because it's actually really nice.
I am trying to make the player look like he is turning when I rotate him Left Or Right with the mouse, or else he just stands in his idle position while rotating him and it doesn't look very realistic.
Edit: I $$anonymous$$eant Give The Illusion He Is Turning, The Anim I Am Using Doesn't actually Turn him It Pivots his foot and if I am rotating the mouse left I want him to pivot his foot left and the same for Right.
stop manipulating animations through code manually like this.
it's clunky, ugly, and weak in features.
we've got built-in animator state machines for 6 years or so by now, so finally start suggesting those as the standard solution, as you should
I am not going to take the time to tell him how to set all that up just like you couldn't be bothered in your answer.
SH_CODE , ur comments are so toxic all over the questions , not sure why u have high reputation .
such a disgusting person!
u can write to him " check State machine" rather than insulting ..etc , seems ur mother need to $$anonymous$$ch u some manner.
Answer by sh_code · Feb 10, 2019 at 08:05 AM
depends on what animation it is and how it should behave and interact with other animations.
this is a huge topic, and your question is too generic.
and the answers you gothere are weak obsolete ugly crap.
go look up some animator state machine/mecanim tutorials instead.