- Home /
Shoot in the direction of an object 2d
I am trying to make a turret that shoots at a specified target. However, all things I have tried have yielded strange results. I first tried from instantiating the bullet from the turrets rotation, which follows the player. This caused the bullet to shoot in a direction that only lined up with the player when above if using transform.up and right with transform.right. When following tutorials online I got even stranger results, with the bullet going in figures of eight. After trying to tweak the scripts, I got it to shoot in the same direction every time. I also tried to change the rotation from both the bullet and shooter side, but all my solutions have failed. I am sorry if this is such a simple problem, but I have found no answer.
Answer by unity_ek98vnTRplGj8Q · Jan 06, 2020 at 03:42 PM
This is generally the approach I would take, although I'm sure there are ways you can do it in less lines of code. This assumes your bullet points to the right by default, but if it doesn't you can tweak the code or the bullet prefab to fix this.
void Shoot(){
//Get direction vector pointing at target
Vector2 directionToTarget = target.transform.position - transform.position;
//This assumes that your bullet sprite points to the right
//Get the angle above the horizontal where the target is
float angle = Vector3.Angle(Vector3.right, directionToTarget);
//This will always be positive, so lets flip the sign if it should be negative
if(target.transform.position.y < transform.position.y) angle *= -1;
//Create a rotation that will point towards the target
Quaternion bulletRotation = Quaternion.AngleAxis(angle, Vector3.forward);
//Spawn bullet
Instantiate(bulletPrefab, transform.position, bulletRotation);
}
Unfortunately, this has not worked. I remain with one of my old problems, even if i remove all other unimportant parts of both the bullet and shooter. The bullets will shoot down if I am above or below it, and up if I am to the left or right. They will also shoot left or right depending on the diagonal. I have experienced this problem many times before, is there something I am doing wrong?
What is your code to actually move the bullet?
public class bullet : $$anonymous$$onoBehaviour
{
public float speed;
void Update()
{
transform.Translate(transform.up * speed * Time.deltaTime);
}
}
Ahhhh took me a while to figure out the issue. transform.Translate()
can take a "Space" parameter, which if not specified defaults to local space ins$$anonymous$$d of world space, which is what we want.
Try this ins$$anonymous$$d: NOTE - In my above code I assumed the default direction for the bullet was to the right, so that's what I have here. If the default direction is actually up, like your code seems to indicate, we may have to change a couple more things
public class bullet : $$anonymous$$onoBehaviour
{
public float speed = 2;
void Update()
{
transform.Translate(transform.right * speed * Time.deltaTime, Space.World);
}
}
Your answer
Follow this Question
Related Questions
bullet shot from Y axis while sprite 2D rotates on z axis 1 Answer
Shooting with variable vector3 0 Answers
Shooting mechanics in 2.5D game 0 Answers
Player jitters when moving. 1 Answer
Gun not firing in certain direction 1 Answer