Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

rotate object to mouse position unity


public class RotateToMouse : MonoBehaviour {
    // public variables
    public float speed = 1.0f; // Set it to whatever you want
    public float distance = 10.0f; // Set it to whatever you want

    // private variables
    private Vector3 mousePos;
    private Vector3 objectPos;
    private float angle;

    // Use this for initialization
    void Start () {
        // get the mouse position
        mousePos = Input.mousePosition;
        mousePos.z = distance;
        objectPos = Camera.main.WorldToScreenPoint(transform.position);
        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;
        angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }

    // Update is called once per frame
    void Update () {
        // get the mouse position
        mousePos = Input.mousePosition;
        mousePos.z = distance;
        objectPos = Camera.main.WorldToScreenPoint(transform.position);
        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;
        angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }

    // rotate the object to the mouse position
    void FixedUpdate() {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }

    // rotate the object to the mouse position (again)
    void LateUpdate() {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    }

    // end of class
}
Comment

rotate to mouse position unity

Please have a look at this tutorial it is really great.
https://www.youtube.com/watch?v=CxI2OBdhLno
Comment

PREVIOUS NEXT
Code Example
Csharp :: C# datetime.now to string only numbers 
Csharp :: linq where list contains another list 
Csharp :: c sharp array to list 
Csharp :: c# connect to mongodb 
Csharp :: c# swap variables 
Csharp :: loop through all enum values in C# 
Csharp :: change vignette intensity unity 
Csharp :: check distance to gameobject 
Csharp :: How to search for a string from readline in c# 
Csharp :: boostrap 4 modal 
Csharp :: unity dictionary check if key exists 
Csharp :: c# unity get lines 
Csharp :: Get Index position of an element in a list in c# 
Csharp :: c# combobox selectedvalue 
Csharp :: how to make a enum list in c# 
Csharp :: new color unity 
Csharp :: how to print a matrix in c# 
Csharp :: C# How to write Hello World 
Csharp :: unity target frame rate 
Csharp :: c# find duplicates in list of strings 
Csharp :: slider.onchanged in unity 
Csharp :: c# write variable in string 
Csharp :: c# create folder 
Csharp :: reverse a string in c# 
Csharp :: c# list to array 
Csharp :: unity vscode no autocomplete 
Csharp :: c# itext 7 pdf add pdf 
Csharp :: vb.net console log 
Csharp :: unity key up 
Csharp :: c# onmousedown. unity 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =