I’ve been trying to understand all the properties for Transform, and I can’t see the difference between a lot of them. I am making a 2D game, and I want enemy projectiles to target the player. What I want them to do it move towards the player, but keep going straight so the player can dodge them. I don’t want them to follow the player around, I want them to pass by, but still be dependent on the position of the player. I hope you can understand what I am saying, but nothing I’ve tried have given me the results I wanted. If you could point me to a Docs page where I can get a better understanding of what I should be doing to achieve this, or if you could provide me a snippet of code from your own experience, that would be great. Thanks!
Set the projectiles direction based on the player position in the frame the projectile is spawned. After that the projectile just moves forward and never gets updated with a new target/direction/whatever.
(can’t see how this has anything to do with transform docs and the rest of your post… I think you’re overthinking it
)
Do you want a projectile to find out where the player is and start firing towards them. But once fired never adjust its position so a player could just move out of the way?
More complicated… do you want to target the player and heat sink towards him but not 100%?
Transform simply represents an object’s current Position, Rotation, and Scale. That’s all you really need to know about it.
If your projectile is 2D, and faces down the X axis (red axis), then all you need to do is create a script for the projectile, which moves forward along that axis every frame.
Then whatever spawns the projectile can point the projectile’s X axis at the player, and the projectile will just blindly move forward from then on.
Here’s some example code of what the spawner might be doing:
GameObject projectilePrefab; // prefab for the projectile
Vector3 spawnLocation; // location to spawn the projectile
Transform player; // reference to the player in the scene
// create a new projectile
GameObject newProjectile = Instantiate(projectilePrefab);
// position it at the spawn location
newProjectile.transform.position = spawnLocation;
// point the projectile's x axis at the player ("right" represents the direction the X axis is pointing)
newProjectile.transform.right = player.position - newProjectile.transform.position;
Then the projectile:
private void Update(){
// move along the "right" axis (x axis) by speed units per second
transform.Translate(transform.right * speed * Time.deltaTime);
}
I feel like I may be overthinking it, but I can’t find a 2D equivalent to LookAt (which would be useful), and I think I could reach my result if that exists. Also @LiterallyJeff the code you provided didn’t work for me, it only fired the projectile the opposite direction it should and the rotation was off, so I obviously have something interfering with the code you provided. Thanks for trying to help though ![]()
if you pass a vector in that has one of the axis zeroed out you have the 2D equivalent to LookAt.
I think @LiterallyJeff code is the 2D equivealent of lookAt
I’m not sure why it is firing in the wrong direction for you… Maybe post the code you put in so we could see
This is good information, so thanks for this. As long as I have looking in the direction of the player, I should be able to move it towards it. I’ll try this method. Thanks guys