I’m trying to make an enemy to move towards my player with this code, but how do I modify it so that it won’t rotate at all ? it is for my 2d horizontal shooter game, my objects are just cubes and sphere with skinned plane parented onto them, so any rotation will break the illusion of 2d view of my game… can anyone help me with this?
bump… hope someone can help me out.
If you take out the rotation, the enemy won’t move toward the player, he will just move forward. Is that what you want?
I tried that… and it doesn’t work… the enemy will walk out of control without going towards the player… anymore suggestion??
That’s what I’m saying, you can’t remove the rotation, because with the current move code you have, the enemy only moves on his forward vector. So in order to reach the player he HAS to rotate so that his forward vector is pointed toward the player.
How do you want the enemy to move toward the player without rotating? Can you describe a game with AI similar to what you’re trying to achieve?
Hello there!
Now this one we can pull off with a few pieces of vector math.
function Update () {
//rotate to look at the player
var dir : Vector3;
dir = target.position - myTransform.position;
dir.Normalize();
//move towards the player
myTransform.position += dir * moveSpeed * Time.deltaTime;
}
Now your enemy will move towards it’s target without rotating at all.
Thanks Myx, I’ll try it out now and let you know the result…
it’s a 2d horizontal shooter game, so far everything working except for the enemy… right now the enemy will just walk from left to right out of the screen and respawn back to the left of the screen… I want it to walk from left to right out of the screen but the direction towards the player (player is at the right end of the screen moving on vertical axis) … I hope it makes sense…
by the way, how do I set the “target” for this script? sorry i’m a beginner, i’m still learning from trial and errors from other’s script from this forum.
oh I get it, that’s the script to replace the script I used earlier… gonna try to make it to work, i have errors but couldn’t figure out where…
var target : Transform;
function Start(){
target=GameObject.Find("Player").transform;
}
function Update () {
//rotate to look at the player
var dir : Vector3;
dir = target.position - myTransform.position;
dir.Normalize();
//move towards the player
myTransform.position += dir * moveSpeed * Time.deltaTime;
}
sorry BigMisterB :), here’s my script for the enemy right now… I got an error of unknown identifier “myTransform” … how do I fix this?
change myTransform to just transform… since you are referring to the current object.
ok, now it says “Unity.Engine.Transform” is required to access non static member “position”
how should I fix this?
What is this script attached to? is it in a class?
it’s attached to a sphere gameobject with rigidbody…
do a simple test…
function Start(){
print(transform.name); // should print the name of the transform...
target=GameObject.Find("Player").transform;
}
where should I put this script? inside my script or a new javascript?
it says “NullReferenceException”
Did you not provide code?