- Home /
Homing Enemies issue (2D)
So i'm trying to make enemies in the form of projectiles that chase the player. The script I have does what I want it to do, the enemies DO follow the player, but they move in ways I don't want them to. More specifically, the game is supposed to be 2D on the X-Y Plane yet to chase the player the projectiles often loop-de-loop into the z-axis. I don't want that, I want them bound to the X-Y plane.
Here is the code (Java)
var target : Transform;
var moveSpeed = 4;
var rotationSpeed = 1;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
myTransform.rotation = Quaternion.LookRotation(target.position - myTransform.position);
}
function FixedUpdate()
{
var lookDir = target.position - myTransform.position;
//lookDir.z = 0;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I know myTransform.forward sends the projectiles into the z-axis, but changing it to right or up causes the projectiles to behave incredibly oddly. The same thing happens when I try to implement the lookDir.z code you see commented out.
Also one last thing: Is there a way to change the movement of the projectile in such a way that the Physics2D material I have attached to it bounces off the rigidbody2D of the player?
So any help? What seems to be the problem?
Answer by Kiwasi · Jul 02, 2014 at 04:32 AM
Add the line
transfom.position = new Vector3(transform.x, transform.y, 0);
This will constrain your object to stay on the plane.
As an aside, why are you caching transform in myTransform?
No reason I suppose, other than I saw it on a tutorial.
So I added the line, and I haven't noticed any change whatsoever. I added it directly below the-
transform.position += transform.forward * moveSpeed * Time.deltaTime;
-line. Should I have added it somewhere else?
Furthermore, I get this in the console. NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Ho$$anonymous$$gJava.FixedUpdate () (at Assets/Ho$$anonymous$$gJava.js:23)
This only happens while the new code is in the game. When I comment it out, nothing appears in the console.
It looks as though the game is searching for a reference, but I have a target object designated, so I don't know what's going on.
Help?
This is the correct place to add it.
What scripting language are you using? In JavaScript I believe you need to drop the new.
I am using Javascript. Removing the "new" from your line of code doesn't change anything again, unfortunately.
Here's what the whole code looks like right now var target : Transform; var moveSpeed = 4; var rotationSpeed = 1;
function Start()
{
target = GameObject.FindWithTag("Player").transform;
transform.rotation = Quaternion.LookRotation(target.position - transform.position);
}
function FixedUpdate()
{
var lookDir = target.position - transform.position;
//lookDir.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
transform.position = Vector3(target.transform.y, target.transform.x, 0);
}
Again, no change. The ball homes in on my player, but I want it bound to the X-Y plane.
You should be using transform.y, not target.transform.y. You also have the x and y reversed. Try replace line 13 with:
transfom.position = Vector3(transform.x, transform.y, 0);
Doesn't do anything, again. All the changes on the above code are a result of me experimenting with it to try and get a different result other than "no change."
Thanks again for your help, by the way.
Your answer
Follow this Question
Related Questions
missile homing script issues 1 Answer
Homing missle with raycast 1 Answer
Homing Missile Target From Parent - Help 1 Answer
Homing mechanic, help! 1 Answer
Quaternion based slerp to make homing projectile doesn't turn 1 Answer