Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Rotating GameObject to mousePosition

Discussion in 'Scripting' started by DeepMan12, Jun 20, 2023.

  1. DeepMan12

    DeepMan12

    Joined:
    Jun 9, 2023
    Posts:
    9
    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)
    Code (CSharp):
    1.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bow: MonoBehaviour
    6. {
    7.     public Vector2 Direction;
    8.  
    9.     public float force;
    10.  
    11.     public GameObject PointPrefab;
    12.     public GameObject[] Points;
    13.  
    14.     public int numberOfPoints;
    15.  
    16.     void Start()
    17.     {
    18.         Points = new GameObject[numberOfPoints];
    19.  
    20.         for (int i = 0; i< numberOfPoints; i++)
    21.         {
    22.             Points[i] = Instantiate(PointPrefab, transform.position, Quaternion.identity);
    23.         }
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         Vector2 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    29.  
    30.         Vector2 bowPos = transform.position;
    31.  
    32.         Direction = MousePos - bowPos;
    33.  
    34.         faceMouse();
    35.  
    36.         for (int i = 0; i< numberOfPoints; i++)
    37.         {
    38.             Points[i].transform.position = PointPosition(i * 0.1f);
    39.         }
    40.     }
    41.     void faceMouse()
    42.     {
    43.         transform.right = Direction;
    44.     }
    45.     Vector2 PointPosition(float t)
    46.     {
    47.         Vector2 currentPointPos = (Vector2)transform.position + (Direction.normalized * force * t) + 0.5f * Physics2D.gravity * (t * t);
    48.         return currentPointPos;
    49.     }
    50. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    I know it doesn't make any sense but it's probably because you're not giving ScreenToWorldPoint a z value.

    Try doing this:

    Code (CSharp):
    1.         Vector3 m=Input.mousePosition;
    2.         Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(m.x,m.y,1.0f));
     
  4. DeepMan12

    DeepMan12

    Joined:
    Jun 9, 2023
    Posts:
    9
    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.
     
  5. DeepMan12

    DeepMan12

    Joined:
    Jun 9, 2023
    Posts:
    9
    Tried it but sadly, there was no change. Do you think it could be because I may be missing a few Unity library files? Though that is highly unlikely
     
  6. DeepMan12

    DeepMan12

    Joined:
    Jun 9, 2023
    Posts:
    9
    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.