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

Resolved looking in direction

Discussion in 'Scripting' started by svenvanh, Jan 20, 2021.

  1. svenvanh

    svenvanh

    Joined:
    Nov 29, 2019
    Posts:
    51
    Hi, I'm working on a little stickman that has eyes.
    I want it so that the eyes look at my mouse. I coded that and it worked.
    but I want it so that the eyes slowly move and not instantly. I'm currently
    using this line of code

    Code (CSharp):
    1. bodyParts[1].transform.position = new Vector3(Mathf.Clamp(mousePos.x / xEyeSensitivity, -0.4f * gameObject.transform.localScale.x, 0.4f * gameObject.transform.localScale.x),Mathf.Clamp(mousePos.y / yEyeSensitivity, -0.6f * gameObject.transform.localScale.x, 0.5f * gameObject.transform.localScale.x) -1f,bodyParts[1].transform.position.z);
    The Mathf.clamp is so the eyes don't go too far from his head, the xEyeSensitivity/yEyeSensitivity are there so
    the eyes don't follow my mouse and the * gameObject.transform.localScale.x is there so when I scale up my sttickman things stay in place.

    I thought I need to do something with Time.deltaTime but that didn't work,
    does anyone have an idea on how to make the eyes slowly look to the eyes?
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    You can use Mathf.Lerp.

    start position where eyes currently are, target is where you want to look, then some float to specify the rate in which they move.
     
    svenvanh likes this.
  3. svenvanh

    svenvanh

    Joined:
    Nov 29, 2019
    Posts:
    51
    Thanks for the reply!
    I got it to work with Mathf.Lerp and it works really smoothly!
     
    vagelis199 likes this.