- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Alp-Giray-Savrum · Feb 01, 2014 at 01:06 PM ·
transformrandomlookatsmooth
Smooth look at Random Transform ?
Hello Developers, I stuck on my modified code...
var targetsArray : Transform[];
var damping = 6.0;
var smooth = true;
@script AddComponentMenu("Camera-Control/Smooth Look At")
function Start()
{
var target = Random.Range(0, targetsArray.Length);
}
function LateUpdate () {
if (smooth)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
}
so this is what i modified, I want to look at randomly selected transform but debug log gives me that error :
SmoothLookAtRandom.js(18,78): BCE0005: Unknown identifier: 'target'.
So what should i do ?
Comment
Best Answer
Answer by StormSabotage · Feb 01, 2014 at 01:34 PM
Your variable "target" is local and will work only in Start(), you should declare it outside Start() method like that:
private var target = -1;
function Start() {
target = Random.Range(0, targetsArray.Length);
}
function LateUpdate(){
if(smooth && (target != -1)){
var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613132243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why nothing happens??? 2 Answers
Transform.position can't be changed smoothly. 2 Answers
LookAt for child objects 0 Answers
LookAt once 0 Answers