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 when I use LookAt my object dissapears (C#)

Discussion in 'Scripting' started by Lenticularic, Jul 21, 2021.

  1. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    whenever I use transform.LookAt the object dissapears, I have no idea why. I get no errors

    Script:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     Rigidbody2D rb;
    7.     public float speed;
    8.  
    9.     void Start() {
    10.         rb = GetComponent<Rigidbody2D>();
    11.     }
    12.     void Update() {
    13.         transform.LookAt(Input.mousePosition);
    14.         if(Input.GetKey(KeyCode.A)) {
    15.             rb.AddForce(new Vector2(-speed, 0));
    16.         }
    17.         if(Input.GetKey(KeyCode.D)) {
    18.             rb.AddForce(new Vector2(speed, 0));
    19.         }
    20.         if(Input.GetKey(KeyCode.W)) {
    21.             rb.AddForce(new Vector2(0, speed));
    22.         }
    23.         if(Input.GetKey(KeyCode.S)) {
    24.             rb.AddForce(new Vector2(0, -speed));
    25.         }
    26.  
    27.         if(Input.GetKey(KeyCode.LeftShift)) {
    28.             rb.drag = 5;
    29.         }
    30.         if(Input.GetKeyUp(KeyCode.LeftShift)) {
    31.             rb.drag = 0;
    32.         }
    33.     }  
    34.  
    35.    
    36. }
    37.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Two problems with your approach:
    1. Input.MousePosition is a screen space coordinate (it's in pixels on the screen) whereas most objects in your game live in World Space which is a different coordinate system
    2. transform.LookAt doesn't generally work well in 2D becuase the "forward" direction of 2D sprites is straight into the screen. So if you point that at your mouse position... the object usually ends up sideways or backwards. (think paper mario turning around). And most sprites don't render from the back at all.
    Try this:

    Code (CSharp):
    1. Plane p = new Plane(Vector3.back, Vector3.zero);
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3. if (p.Raycast(ray, out float dist) {
    4.   Vector3 mousePos = ray.GetPoint(dist);
    5.   transform.right = ;mousePos - transform.position;
    6. }
    Side note:
    AddForce is best done in FixedUpdate to avoid making your game behave differently at different framerates.
     
    Lenticularic likes this.
  3. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    Thanks a lot, I for some reason didn't think about how 2d sprites worked. Also, I didn't use FixedUpdate b/c when I did, it didn't register the inputs all the time. DO you have any solutions to that? I thought of calling a void every frame in FixedUpdate, but that feels inefficient, and it wouldn't do anything probably.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    As always, read the input in Update(), set a temp variable true if input happens.

    Then in FixedUpdate(), if the temp variable is true:

    - take the action
    - clear the variable
     
    Lenticularic likes this.