How to rotate the player towards a Vector2?
When the player is selected, and you click on the world. The player will move there. However, they do not rotate in the direction they are going. Here is my code:
public class UnitBehavior : MonoBehaviour
{
//Movement
public bool isSelected;
public float moveSpeed;
private Vector3 moveTarget;
public bool isMoving;
//Info
public float health;
public float combatStress;
public string unitName;
// Start is called before the first frame update
void Start()
{
moveTarget = transform.position;
}
// Update is called once per frame
void Update()
{
//Health
if(health <= 0)
{
//dies
}
//Movement Order
if (Input.GetMouseButtonDown(1) && isSelected)
{
moveTarget = Camera.main.ScreenToWorldPoint(Input.mousePosition);
moveTarget.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, moveTarget, moveSpeed * Time.deltaTime);
}
//Selection
void OnMouseDown()
{
if(isSelected == false)
{
isSelected = true;
} else if(isSelected == true)
{
isSelected = false;
}
}
//Enemy in view
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{
}
}
}
Comment
Answer by Vega4Life · Aug 01, 2019 at 10:58 PM
You probably just want to look in the direction you are going so.. something like this might work for you:
Vector3 dir = moveTarget - transform.position;
transform.LookAt(dir);
Just try sticking something like that where you are moving your player.
@Vega4Life Sorry, I meant Vector2. I want the player to rotate towards a Vector2 which stores the point where I click on the screen. The player already moves there, now I just need it to rotate in that direction. when I put in your recommended code, It just causes the player object to rotate out of view of the orthographic camera.