Search Unity

I can't get this script to work.

Discussion in 'Scripting' started by Treasureman, Mar 4, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a code that's supposed to to make my camera rotate to the same x axis as an empty gameobject called "CameraRestricted_Axis" when the input "Aim" is held. Here's the script...
    Code (JavaScript):
    1. #pragma strict
    2. @SerializeField
    3. var aimSpeed : float;
    4. function Update () {
    5.     if (Input.GetButton("Aim"))
    6.         Aim();
    7. }
    8. function Aim() {
    9.     var aimRotation : Vector3 = transform.rotation.eulerAngles;
    10.     aimRotation.x = CameraRestricted_Axis.transform.rotation.eulerAngles.x;
    11.     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(aimRotation), Time.deltaTime * aimSpeed);
    12. }
    But I can't get it to work. How would I fix it?
     
    Last edited: Mar 4, 2015
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Is aimSpeed > 0?
    If you haven't set it in the inspector than it will be 0.
    Other than that I see no problems as long as this script is attached to the camera.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Don't use Time.deltaTime as an argument in Lerp. You're basically just setting it to a value very near the original rotation.
     
    karljj1 likes this.
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    That is the effect, to give it smoothing. I do this all the time, with great results. The problem would be if aimSpeed is too low a value, then it wouldn't move very fast and if it is 0 it wouldn't move at all.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That pattern arose out of bad documentation on Unity's part (which thankfully they've now corrected). What you have now will never reach the intended target because the final argument is a percentage of completion. It "works" because you're moving the rotation a very small amount each time. The problem is, that small amount gets smaller as the 2 values approach each other.

    To illustrate - Imagine I do a Lerp(0, 1, 0.5). The result is 0.5. Then I do it again so Lerp(0.5, 1, 0.5). Now the result is 0.75. I moved 0.5 the first time and 0.25 the second time. If I do it again the result is 0.875. I'll never get to 1.
     
    karljj1 likes this.
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Same goes for dividing any number by half and if you keep halving the result, you will never see 0.
    It will still reach near the value, yes it will never be 1 but 0.9997 is so close you couldn't tell the difference.

    This effect, while not the intended purpose of the lerp function, is similar to a power curve. You could use SmoothDamp, or even do the power curve yourself and achieve the same results. One of the reasons I like programming, is that you can do it anyway that "works". I do agree that lerp should be used to linearly interpolate between 2 values, like using the right tool for the job, and if you want smoothing use a function more suited to that.

    Perhaps providing an alternative solution may help.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539