- Home /
rot not working for 2nd prefab
This script is attached to a game object prefab. When I spawn the prefab, it rotates toward my player. When I spawn a 2nd prefab, the rotation no longer functions for the 2nd prefab ONLY. The first prefab still continues rotating correctly. What am I doing wrong? Why would it work for the 1st spawned game object but not the 2nd, 3rd, etc?
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, .1);
function Start()
{
target = GameObject.FindWithTag("aggro").transform; //target the player
myTransform = this.transform;
}
function Update () {
myTransform = this.transform;
//rotate to look at the player
this.myTransform.rotation = Quaternion.Slerp(this.myTransform.rotation,
Quaternion.LookRotation(target.position - this.myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
this.myTransform.position += this.myTransform.forward * moveSpeed * Time.deltaTime;
}
Any help is greatly appreciated. Thank you.
What I meant was, where do you set the transform you want to look at?
You have myTransform
set, but I don't see where you say who target
is.
oh i'm sorry. allow me to put the whole script. i only included the portion I was referencing because I thought it would make it easier to diagnose. i'm wrong though =p I just updated it.
$$anonymous$$, View some responses to your question in the other thread you started: http://answers.unity3d.com/questions/371797/restart-this-script.html
Answer by aldonaletto · Dec 31, 2012 at 04:46 PM
I don't know why this script is working for the first enemy only - actually, it should not work for any one, maybe not even compile! You can't use many game object functions or properties in code outside functions, causing errors in the 1st and 4th lines, and calling Start and Update via InvokeRepeating definitely seems wrong - the engine calls them at the appropriate times (Time.deltaTime has no meaning if you call Update yourself). You should also declare moveSpeed and rotationSpeed as floats (or add a decimal point to the initialization values), because the way they're declared make them integers, possibly making some expressions return 0.
You should remove the InvokeRepeating lines and modify the variable declarations like below:
var target: Transform; // just declare the variable type
var moveSpeed: float = 10; // it's better to declare this as a float
var rotationSpeed: float = 5000; // idem (the enemy rotates 5000 degrees per second!?!)
var myTransform: Transform; // again, just declare the variable type
Remove also the first line of Update: you've already assigned myTransform at Start. Try these changes and let us know if the problem still persists.
thanks for the help. it is still not working. unfortunately the line "var myTransform: Transform;" is throwing an error "cannot convert 'UnityEngine.Transform' to 'System.Type'"
this is what i get for trying to edit scripts myself. everyone see's my terrible errors and now the problem seems worse than before. i would have been better not modifying the script at all it seems. i appreciate your help. i'll just have to take this functionality out entirely since it just doesn't work =( i was so close too!
Weird thing! This line is just a variable declaration - are you sure the error is in this line?
Answer by deltamish · Jan 01, 2013 at 01:54 AM
Hi,like he said dont decalre object properties outside the function and when are instantiating the objects because you have specefied the target in Start function. so the object only works when you instantiate it at the start but if you instantiate it during an update it will rotate towards the player
Just change from
function Start()
{
target = GameObject.FindWithTag("aggro").transform; //target the player
myTransform = this.transform;
}
function Update () {
myTransform = this.transform;
//rotate to look at the player
this.myTransform.rotation = Quaternion.Slerp(this.myTransform.rotation,
Quaternion.LookRotation(target.position - this.myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
this.myTransform.position += this.myTransform.forward * moveSpeed * Time.deltaTime;
}
To
function Start(){
mytransform = transform;
}
function Update(){
target = GameObject.FindWithTag("aggro").transform;
//your other code
}
now it will work as the object checks for the player every frame
by the way Happy New Year To One And All. May this year be sucessful to you