Hey I recently started using Unity and am trying to design a flash-like arrow shooter game. I want to make my bow snap to the direction of the mouse but it only works on startup even though its in the Update() function. Any ideas on how to fix this? I’ve attached the code. (All credits for code go to Cezary _Sharp on youtube) ``
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bow: MonoBehaviour
{
public Vector2 Direction;
public float force;
public GameObject PointPrefab;
public GameObject[] Points;
public int numberOfPoints;
void Start()
{
Points = new GameObject[numberOfPoints];
for (int i = 0; i< numberOfPoints; i++)
{
Points[i] = Instantiate(PointPrefab, transform.position, Quaternion.identity);
}
}
void Update()
{
Vector2 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 bowPos = transform.position;
Direction = MousePos - bowPos;
faceMouse();
for (int i = 0; i< numberOfPoints; i++)
{
Points[i].transform.position = PointPosition(i * 0.1f);
}
}
void faceMouse()
{
transform.right = Direction;
}
Vector2 PointPosition(float t)
{
Vector2 currentPointPos = (Vector2)transform.position + (Direction.normalized * force * t) + 0.5f * Physics2D.gravity * (t * t);
return currentPointPos;
}
}
I’ve tried both your methods, and though I like the Math.Atan2 approach, the problem persists. I tried to attach a video of what happens when I’ve used the Atan2 approach but direct uploads aren’t allowed it seems
I’ve also tried using the Debug.Log to check if the Update() function is continuous in its activity and, it is. So I’m just really puzzled here.
Update: I was able to solve the problem by creating a new GameObject and just copy pasting the code, it works now. For some reason, the old GameObject’s rotation was broken, I have no idea why.