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 Mouse Aiming Rotation Problem

Discussion in 'Scripting' started by GMGambit03, Aug 1, 2023.

  1. GMGambit03

    GMGambit03

    Joined:
    Aug 1, 2023
    Posts:
    4
    To begin I'm trying to make a floating staff follow the direction of the mouse pointer. So far I got that to work using the following code:

    public Vector2 pointerPosition { get; set; }

    // Update is called once per frame
    void Update()
    {
    Debug.Log(pointerPosition);
    transform.right = (-pointerPosition - (Vector2)transform.position).normalized;
    }

    I have another script to get the mouse position and all that but whenever it rotates around the character I want the staff to continue pointing up. I have the actual staff object as a child to an empty game object that's rotating.

    I'm not sure if I made that make sense so if there anything you need me to clarify or explain I will.
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    Sorry, It doesn't make sense to me. I have no idea what exactly are you trying to ask or achieve. You will have try explaining again.

    You previously said you want the staff to point at mouse and got that working already. Now you are telling that you want the staff to point up instead of at mouse. Where exactly do you want the staff to point?

    Is this important to whole rotation aspect or is it just a script which set's the pointerPosition property for the script you pasted above? If it does more than just reading and writing mouse position and it is relevant for the visuals you are trying to achieve it might help if you explained what "it" is. It's hard to understand the situation involving multiple objects if some of them are unnamed only referred as "another thing" and "it" .

    My last attempt interpreting what you asked - Do you want the staff to point at mouse only sometimes and point up rest of the time? Depending on some other object which is also rotating around player.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    FYI: I removed the 2D physics tag as it's not related. Also, this isn't a post for the 2D forum as it's simply a generic scripting question. 2D features don't include the mouse or the Transform system.
     
  4. GMGambit03

    GMGambit03

    Joined:
    Aug 1, 2023
    Posts:
    4
     

    Attached Files:

  5. GMGambit03

    GMGambit03

    Joined:
    Aug 1, 2023
    Posts:
    4
    I didnt kniow where would i post this questions
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1. public Vector2 pointerPosition { get; set; }
    2.  
    3. void Update()
    4. {
    5.     transform.LookAt(pointerPosition,Vector3.up);
    6. }
    7.  
     
  7. GMGambit03

    GMGambit03

    Joined:
    Aug 1, 2023
    Posts:
    4

    Thankyou it worked