- Home /
Question by
Scary-Bob1 · Jan 23, 2020 at 09:44 PM ·
2dglitchmouselookrotatearound
How to stop object from glitching when coming too close to mouse?
I am making a top down 2D planet defense game. I have a player object rotating around a planet at (0,0,0) and the player rotates around the planet while looking at the mouse. However, when the mouse comes within 4 units of the player on the x axis, the player splits into two objects and starts glitching really badly. It's like the player is splitting in two because it doesn't know where to rotate once the mouse gets too close. Is there anyway to fix this?
Here is the script.
public Transform planet;
private Vector3 v;
// Start is called before the first frame update
void Start()
{
v = (transform.position - planet.position);
}
private void FixedUpdate()
{
//look at mouse
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 lookDir = mousePos - transform.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.position = planet.position + q * v;
transform.rotation = q;
Debug.Log(lookDir);
}
Comment
Your answer
Follow this Question
Related Questions
Rotate Around Planet with Spaceship Face Set on Path 1 Answer
Top Down 2D Mouse Look 0 Answers
2D Autoscroller Glitches 0 Answers
Basic 2d movement problem: glitching in terrain 0 Answers
Weird 2D Mouse Look At Problem 2 Answers