# Why is the first click ignored on this script?

Shouldn’t this rotate right at the first click? But the first click is ignored. Why?

using UnityEngine;
using System.Collections;

public class ClickRotate_HBL_v07 : MonoBehaviour {

public float rotationStepAngle = 30.0F;
public float timer = 10.0F;

private float rotationToAngle;
private bool doRotateL = false;
public float snappingThreshold = 0.01F;

void Start () {
float rotationToAngle = rotationStepAngle;
}

void LateUpdate () {

if(Input.GetMouseButtonDown(0)){
doRotateL = true;
}

Rotator();
}

void Rotator()
{
if(doRotateL){

Quaternion newRotation = Quaternion.AngleAxis(rotationToAngle, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * timer);

if((transform.rotation.eulerAngles - newRotation.eulerAngles).magnitude < snappingThreshold) {
// if snappingThreshold is passed

transform.rotation = newRotation;
// snap object rotation to exact value

doRotateL = false;

rotationToAngle += rotationStepAngle;
// set next angle to be targeted
}
}
}
}

I don’t know why, but at a first glance this line is wrong:

`// snaps to exact value of newRotation after Lerp approximation
transform.rotation = newRotation;`

or perahps useless…

in fact you wouldn’t enter in the if section if this isn’t already true.

I think that you would set a threshold as if condition in the following way:

if((transform.rotation.EulerAngles - newRotation.EulerAngles).magnitude < threshold)

EDIT: I found it! ^^
you set rotationToAngle locally in the Start function!
so it’s 0 at the first click.

you must delete “float” at line 16

So just for the record here’s the working script. Thanks again!

using UnityEngine;
using System.Collections;

public class ClickRotate_HBL_v07 : MonoBehaviour {

public float rotationStepAngle = 30.0F;
public float timer = 10.0F;

private float rotationToAngle;
private bool doRotateL = false;
public float snappingThreshold = 0.01F;

void Start () {
rotationToAngle = rotationStepAngle;

}

void LateUpdate () {

if(Input.GetMouseButtonDown(0)){
doRotateL = true;
}

Rotator();
}

void Rotator()
{
if(doRotateL){

Quaternion newRotation = Quaternion.AngleAxis(rotationToAngle, Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * timer);

if((transform.rotation.eulerAngles - newRotation.eulerAngles).magnitude < snappingThreshold) {
// if snappingThreshold is passed

transform.rotation = newRotation;
// snap object rotation to exact value

doRotateL = false;

rotationToAngle += rotationStepAngle;
// set next angle to be targeted

}
}
}
}