- Home /
Why is my program not working properly ?
Hello There, I'm trying to program an animation but I have problem. My animation have 3 part. Firstly, my camera is going somewhere I chose before and it's waiting on there 10sec. Secondly, the camera should go somewhere I chose in a different position but my counter is increasing immediately without wait the second step and it's going to 3rd step. Lastly,the third part is also that camera should go to a place I chose differently. And it's going there.
Why my counter increasing without wait in the 2nd Sec.
My codes:
using UnityEngine; using System.Collections;
public class move : MonoBehaviour {
public Transform target;
public int mSpeed=1;
public int rSpeed=1;
private GameObject go;
private float pre;
private int pos=0;
private Transform myTransform;
// Use this for initialization
void Awake(){
myTransform=transform;
}
void Start () {
pos=1;
print(pos.ToString());
go=GameObject.FindGameObjectWithTag(pos.ToString());
target=go.transform;
}
// Update is called once per frame
void Update () {
myTransform.rotation=Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position),rSpeed*Time.deltaTime);
if( (myTransform.position-target.position).magnitude>10){
myTransform.position += myTransform.forward*mSpeed*Time.deltaTime;
}
if((myTransform.position-target.position).magnitude<=10)
{
Invoke("PosPlus", 2);
}
}
void PosPlus (){
if(pos==1){
print("pos was:"+pos.ToString());
pos=2; }
else if(pos==2){
print("pos was:"+pos.ToString());
pos=3; }
print(pos.ToString());
go=GameObject.FindGameObjectWithTag(pos.ToString());
target=go.transform;
Invoke("Update",0);
}
}
Use the code button to format code when you post here please.
Answer by sparkzbarca · Nov 10, 2012 at 08:15 AM
on function start store timer.time as startingtime
//the anding just allows for it to be approximately you could narrow it down alot if you //wanted to. right now it just makes sure it only triggers between 10 and 11 seconds.
//you can just do greater than, or the 20 second mark would trigger it as well to go
//to pos 1 and then you want it go to pos 2.
if (starting + 10 < timer.time && starting + 11 > timer.time)
{
go to pos 1;
}
if(start + 20 < timer.timer && starting + 21 > timer.time)
{
go to pos 2
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613083627im_/https://answers.unity.com/themes/thub/images/avi.jpg)