2D Object rotation after creation doesn't work (depending on mouse position)

Hey guys,

I am currently working on a script that should spawn a 2D object (a paddle) at the position where the mouse is clicked. However, once the mouse is up, the object should rotate to the point where it was “up”.

I got the rotation code from an answer here on the website after reading around 2 dozens of comments about the subject (and this looked the easiest and the only one I could understand most of it). The problem is that the rotation isn’t correct at all. It has nothing to do with the mouse’s location when I let go of the click.

At first I thought it might be a problem with my sprite and it isn’t facing forward (as some answers suggested) and I subtracted the whole “angle” part by 90 and it still doesn’t work at all. Any ideas what I am doing wrong or if there is a simple way to do this?

Note: I don’t want the object to keep rotating, only rotate towards the location where the mouse is let go and doesn’t do anything else, that is it.

Here is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour {
    
    
    public GameObject paddlePrefab;

    
    private int numberOfPaddles = 3;
    private List<GameObject> paddles = new List<GameObject>();
    private GameObject tempPaddle; // This is to store the paddle temporarily so as to remove it from list and add it back again so it is last 
    private GameObject paddle;
    private Vector3 direction;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
            if (paddles.Count < numberOfPaddles)
            {
                paddle = Instantiate(paddlePrefab, Camera.main.ScreenToWorldPoint(mousePos), Quaternion.identity) as GameObject;          
                paddles.Add(paddle);
            } 
            else
            {
                tempPaddle = paddles[0];
                tempPaddle.SetActive(false);
                paddles.RemoveAt(0);
                tempPaddle.SetActive(true);
                tempPaddle.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
                paddles.Insert(numberOfPaddles - 1, tempPaddle);
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            Vector3 mouseUpPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
            CalculateAngle(mouseUpPos);
        }
    }

    void CalculateAngle(Vector3 mouseUpPos)
    {
        direction = mouseUpPos - paddle.transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * (Mathf.Rad2Deg);

        if (paddles.Count < numberOfPaddles)
        {
            paddle.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }
        else
        {
            tempPaddle.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }
    }
}

if i have gotten this right you want to hold down the mouse then once you release it, it rotates torwards the point where it was released right.
if that’s the case then the error is
that when you say

 input.GetMouseDown

that means the frame that the user clicks the button, not while you hold it down so instead you should write

input.GetMouse

which means it calculates the position of the mouse every frame
so what your script is doing right now is finding the mouse position of where you clicked and then rotating it torwards the location where you clicked once you released the mouse.
hope this was what you needed :slight_smile: