- Home /
Mouse rotation, jerky movement
I have this script that rotates the player around with the mouse, with a no rotation camera 45deg top down view. But if I move the mouse close to the player it snaps to the different mouseInput direction. How can I make the player turn smoothly and not 'snap'?
Also, is there a way to determine when the player turns for the animator animations? As of now it only plays the walk animation if you walk forward or backwards (WS keys).
public float speed = 10f;
public float rotationSpeed = 100f;
public Animator animPlayer;
void Start (){
animPlayer = GetComponent<Animator>();
}
void FixedUpdate () {
Vector3 mousePos = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, -angle, 0));
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
animPlayer.SetFloat("Speed", translation);
}
Thanks!
Answer by THE-Scientist34 · Jun 12, 2017 at 08:45 AM
yo try this works good for me. using UnityEngine; using System.Collections; public class MouseFollow : MonoBehaviour { public Camera h; public float speed; private Vector3 mousePosition; private Vector3 direction; private float distanceFromObject; void FixedUpdate() { if (Input.GetButton("Fire2")){ mousePosition = h.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z - h.transform.position.z)); GetComponent<Rigidbody>().transform.eulerAngles = new Vector3(0,0,Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x))*Mathf.Rad2Deg - 90); } } }
Your answer
![](https://koobas.hobune.stream/wayback/20220612123515im_/https://answers.unity.com/themes/thub/images/avi.jpg)