2D Sprite issue when rotating towards mouse click location.
Hello there, I am fairly new to Unity, but I am experiencing a slight issue when rotating my sprite towards a mouse click location in a 2D top-down scene. The sprite will move towards the location and rotate accordingly, however upon reaching this location, its rotation changes back to its original transform rotation when the scene was started. I have tried a few different rotational methods but all of these generally have the same issue, which leads me to think its a program structure problem. Ideally I would like the sprite to move and rotate to the location clicked or where the mouse is currently being held down, and then to stay at the rotation that it was moving at when it reaches the destination.
Here's my code to look through, any help on the matter would be much appreciated. Thanks.
using UnityEngine;
using System.Collections;
public class Cat : MonoBehaviour
{
public float speed = 1.5f;
private Vector2 moveLocation;
private Vector3 mouseRotation;
private Vector3 mousePosition;
private Vector3 newPos;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)||Input.GetMouseButton(0))
{
//Save the position of the location clicked
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
moveLocation = new Vector2(mousePosition.x, mousePosition.y);
//Save the rotation of the mouse relative to the z-axis of Cat
mouseRotation = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - transform.position.z));
}
//Update the Cat's position based on the mouse click location
transform.position = Vector2.MoveTowards(transform.position, moveLocation, speed * Time.deltaTime);
//Update the Cat's rotation based on mouse click location
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePosition - transform.position);
//transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * Mathf.Rad2Deg - 90);
//Quaternion.AngleAxis(Mathf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * Mathf.Rad2Deg - 90, Vector3.forward);
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, Mathf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * Mathf.Rad2Deg - 90), 2.0f * Time.deltaTime);
}
}
It may seem slightly messy with the coding but this seems to have solved the problem, a big thanks to Salocin19 for pointing out the issue and providing a solution to the problem.
See below for the code used for anyone else experiencing a similar problem.
//Check for position and mouse click location meeting so that we don't rotate unnecessarily
if ((transform.position.x != mousePosition.y) && (transform.position.y != mousePosition.y))
{
//Update the Cat's rotation based on mouse click location
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, $$anonymous$$athf.Atan2((mouseRotation.y - transform.position.y), (mouseRotation.x - transform.position.x)) * $$anonymous$$athf.Rad2Deg - 90), 2.0f * Time.deltaTime);
}
Answer by Salocin19 · Mar 05, 2016 at 02:50 AM
I believe the problem here is the following scenario:
-Cat has reached mouse location
-Cat then tries to update its rotation with :
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePosition - transform.position);*
-BUT, at this point, the cat's position and the mouse's position are the same.
-Therefore, the rotation will be set to 0 because mousePosition - transform.position = 0.
One possible way to fix this:
-Before updating the rotation, check to see if the Cat's position and the mouse position are the same. If so, go ahead and update rotation. Otherwise, don't.
Hope this helps!
Answer by DBarnes94 · Mar 05, 2016 at 03:49 PM
Brilliant that could very well be it, i'll take a crack at it the next chance that I get and let you know if it does indeed work. Thanks.