- Home /
Restart this script
How can I restart this script when it's finished running? I just want it to repeat the entire script over and over infinitely. Here is the script:
var target : Transform; //the enemy's target
var moveSpeed = 10; //move speed
var rotationSpeed = 310; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("aggro").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Any help is greatly appreciated. Thank you.
Answer by GlennHeckman_old · Dec 31, 2012 at 06:05 PM
Shawn, Here are some tips for a more efficient script, and hopefully and answer to your question:
There is no need to save the enemy's transform data into a variable, you can just access it directly by typing:
transform.position
It is silly to call a repeat of a function every 1/Billionth of a Second, 1/10 or so will work just fine (.1).
Look into Coroutines as well if you think you'll need to do a lot of looping stuff: Coroutine
There is no need to InvokeRepeating an Awake or Start function. They are meant to only run one time.
InvokeRepeating an Update function is a waste of processor power and is pointless because the Update function will repeat itself anyways. It's what it does; similar to OnEnterFrame for ActionScript.
If you want to repeat a function, create a new one and call that. Here's an example: > function Start() > { > InvokeRepeating("TestFunction",5); > } >
> function TestFunction() > { > print("Test Function every 5 seconds"); > }You should type cast your variables so it makes it easier for debugging, and overall readability of your code. > var target:GameObject; > var moveSpeed:int = 10; > var rotationSpeed:int = 5000;
Your Start function is where you should declare your variables: > function Start() > { > // Enemy's Target > target = GameObject.Find("aggro"); > }
The Update function is where you want code to execute every frame (like ActionScript EnterFrame).
> function Update () > { > //rotate to look at the player > transform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.transform.position - transform.position), rotationSpeed*Time.deltaTime); >
> //move towards the player > transform.position += transform.forward moveSpeed Time.deltaTime; > }
Hope this helps!
Glenn
thanks Glenn. you are right. i moved everything into the Update function and it magically works now. accepting your response as correct.
Answer by ZorNiFieD · Dec 31, 2012 at 01:06 AM
The script does continue running over and over. Anything in the Update() routine is running repeatedly. What is not repeating for you?
If you want to target the player again, just move the line:
target = GameObject.FindWithTag("aggro").transform; //target the player
from the Start function to the Update function.
hey I want to accept GlennHeckman's answer ins$$anonymous$$d of this answer. it won't let me remove the accepted status from this question and give it to Glenn's question. any thoughts?
Answer by Velketor · Dec 31, 2012 at 02:34 PM
Thanks for the help. I found it's really a different issue for me. Instead of searching for all the gameObjects by tag, I found it would be better to reference the owner of the object instead (since that is who I am looking for). Here is the updated script:
var target = GameObject.Find("Aggro").transform; //the enemy's target
var moveSpeed = 10; //move speed
var rotationSpeed = 5000; //speed of turning
var myTransform = this.transform; //current transform data of this enemy
InvokeRepeating("Start", .1, 1);
InvokeRepeating("Update", .1, .0000000001);
InvokeRepeating("Awake", .1, .0000000001);
function Awake()
{
myTransform = this.transform;
}
function Start()
{
target = GameObject.FindWithTag("aggro").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
However, it is still only working for the FIRST prefab that is spawned. After that, the script doesn't execute the rotation for new gameObjects. Basically, this script is only working on 1 object, the first spawned. I'll accept your answer since it was correct. I am now switching up my question a bit and I hope you can help answer that too =) thanks good sir.
Sorry, but that makes no sense what you did there. Awake, Start and Update are special callbacks that are called automatically when it's time to. You really shouldn't call those functions manually or by using InvokeRepeating on them.
Update is called every frame so calling it again doesn't make any sense.
It seems you want to search for a new target every second, in this case create your own function for that but don't use Start.
Use meaningful names, unity did the same. Start is called once at Start of the game / lifetime of the script. Update is called when the script needs to be updated which is once per frame.
I guess you want something like that:
var target : Transform; //the enemy's target var moveSpeed = 10.0; //move speed var rotationSpeed = 5000.0; //speed of turning private var myTransform = this.transform; //current transform data of this enemy
function Start()
{
myTransform = this.transform;
InvokeRepeating("FindNewTarget", 0.001, 1.0);
}
function FindNewTarget()
{
target = GameObject.FindWithTag("aggro").transform;
}
function Update ()
{
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
thanks for the help. although I received no errors, the updated script you provided still functions exactly the same as my old script. the 2nd prefab has no problem moving toward my player. it has a problem rotating toward the player. it will rotate toward the player for a split second and then it looks back it's normal direction and then it repeats that over and over.
That doesn't make sense. If my script does something else than yours there has to be another script that cause this. Are you sure you don't have two scripts which might control the same object?
What you did is just wrong. It's like you say "i drive my car backwards". Of course it works but it's incredible inefficient and may have unwanted sideeffects.
It is only 1 script. If I am driving my car backwards then I apologize. I don't know how to do it properly yet and I am still learning. Thanks for trying to help me.
Your answer
Follow this Question
Related Questions
functions file and NullReferenceException: Object reference not set to an instance of an object 1 Answer
Activate/Deactivate Script from another Object. 3 Answers
How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers
How to call a function from a script in another scene 5 Answers