Question by
Kayomn · Oct 18, 2015 at 10:57 AM ·
physics2d game2d-physicsmouselookmouse position
2D Top-Down Shooter Glitch With Camera Focus
So I have an issue with my code for orientating the player towards the mouse. The glitch occurred when the player character clashes with an enemy AI, or any AI that's trying to push them while you're trying to turn with the mouse. The issue is that the mouse will continue moving while the player will not be trying to recentre itself on looking at the mouse.
After moving away from the opposing force the mouse will be off centre and the player. Any ideas on fixes? Any help would be deeply appreciated.
public float rotateSpeed = 50.0f;
void FixedUpdate() {
var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var dir = transform.position-mousePosition;
var angle = Mathf.Atan2(dir.y,dir.x)*Mathf.Rad2Deg;
var newRotation = Quaternion.AngleAxis(angle+90,Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation,newRotation,Time.deltaTime*rotateSpeed);
}
Comment