Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity player look at mouse

private void Update()
    {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 5.23f;

        Vector3 objectPos = UnityEngine.Camera.main.WorldToScreenPoint(transform.position);
        
        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;

        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90));
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity android quit application 
Csharp :: c# generate random date 
Csharp :: random bool c# 
Csharp :: play a sound c# 
Csharp :: base64 to image c# 
Csharp :: c# check if string is empty 
Csharp :: c# loop through datatable 
Csharp :: c# append to file 
Csharp :: current directory in sln file c# 
Csharp :: unity why is there no transform.left 
Csharp :: convert int to double c# 
Csharp :: unity pause animator 
Csharp :: unity textmesh pro 
Csharp :: c# remove last value from list 
Csharp :: c# difference between break and continue 
Csharp :: c# change label value into int 
Csharp :: c# private set 
Csharp :: c# form formborderstyle none move 
Csharp :: how to make rb.addforce 2d 
Csharp :: c# counting lines 
Csharp :: c# how to convert string to int 
Csharp :: unity ui change sprite 
Csharp :: db scaffolding ef core 
Csharp :: currentTimeMillis c# 
Csharp :: c# determine configration at runtime 
Csharp :: nepali phone number regex 
Csharp :: how to turn off sprite renderer in unity 
Csharp :: how to compare 2 date time in asp.net core 3.1 
Csharp :: c sharp check if key in dictionary 
Csharp :: ensuresuccessstatuscode exception 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =