I have a small arrow sprite that I would like to rotate towards where the player shoots (in a top down hockey game). The player shoots in the opposite direction of mouse drag. The current indicator I have seems to point in arbitrary directions. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class indicatorScript : MonoBehaviour
{
[SerializeField] GameObject puck;
puckShoot puckScript;
[SerializeField] GameObject player;
playerscript playerScript;
[SerializeField] Transform goal;
//Vector2 dir;
Vector2 startPoint;
Vector2 currentPoint;
void Start()
{
puckScript = puck.GetComponent<puckShoot>();
playerScript = player.GetComponent<playerscript>();
}
private void OnMouseDown()
{
if (playerScript.hasPuck && playerScript.mouseDrag)
{
startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
private void Update()
{
if (playerScript.mouseDrag && playerScript.hasPuck)
{
currentPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var dir = startPoint - currentPoint;
var angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
I can attach the shooting script as well if that would be helpful. Thank y’all so much!