0

So I have a turrent on a swivel created in blender that I would like to have rotating back and forth in a preview screen so I got this so far

public float speed = 1.5f;

private GameObject turrent;

private Quaternion localRotation;

private float maxVal = 140f;
private float minVal = 55f;

private void Start(){
    turrent = GameObject.FindGameObjectWithTag ("Player");
}

private void Update(){
    localRotation.y = Mathf.Clamp (speed, minVal, maxVal);
    turrent.transform.rotation = localRotation;
}

I thought this would make it go back and forth between the 2 values but it just snaps to 140 degress in y and doesn't go back and forth.

What am i doing wrong?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
FlamingGenius
  • 216
  • 3
  • 22
  • 1
    Clamp just makes smaller-than-minimum values to be minimum and larger-than-maximum values be maximum. You'll have to write the logic yourself that makes it go back and forth. In the simplest way add a variable that you add to the angle and when it goes over max or under min negate its value – Sami Kuhmonen May 26 '17 at 14:23
  • 1
    Use the Lerp function, if I remember correctly it is also in the Mathf library. – Headhunter Xamd May 26 '17 at 14:24
  • 1
    The accepted answer on the duplicate worked for me! – FlamingGenius May 26 '17 at 14:46

0 Answers0