- Home /
How to make projectiles shoot diagonally?
Hello all, Im trying to make a 2D "spread shot" where 3 projectiles are instantiated in 3 different directions. One straight forward and 2 diagonals up and down. I feel like there is a simple way to do this that i'm not figuring out.
Here's my code:
public GameObject Bulletprefab;
void Update ()
{
if (Input.GetKeyDown (KeyCode.RightShift))
{
Shoot ();
}
}
public void Shoot()
{
GameObject Bullet = Instantiate (Bulletprefab, transform.position, transform.rotation);
}
}
Answer by Kciwsolb · Apr 19, 2018 at 06:51 PM
There are lots of ways you could do this. An easy visual way is to create a reference to 3 public Transforms in your code.
public Transform shootDirection1, shootDirection2, shootDirection3;
Then create 3 empty GameObjects in your scene (probably as children of your gun). Line these up so they point in the directions that you want your projectiles to shoot out (one with the z axis pointing forward, one angled up, one angled down). Then drag a reference of the Transform of these GameObjects to the public Transforms on your script. Then in your script make 3 different Instantiate calls for each of your shooting directions:
Instantiate (Bulletprefab, shootDirection1.position, shootDirection1.rotation); //Do this for each shootDirection
And there you go! You should be shooting in 3 different directions! :)
The projectile doesn't travel in the diagonal direction, it just gets instantiated rotated on an angle. I want it to fire and move on a diagonal path
I used your idea to get the rotation, then I used a vector 3 for the speed and combined X and Y speeds to get the diagonal.
$$anonymous$$y only goal was to get it instantiated and rotated properly. I assumed you had a script for your projectile that was moving it along its local forward (z) axis. If you don't have a separate script for that, here is a line of code you could add that would work (it assumes your projectile prefab has a Rigidbody):
/*Do the below for each shootDirection*/
Rigidbody projectile1Rb = Instantiate (Bulletprefab, shootDirection1.position, shootDirection1.rotation).GetComponent();
projectile1Rb.AddRelativeForce(new Vector3(0.0f, 0.0f, projectileSpeed), Force$$anonymous$$ode.VelocityChange);
Okay so, I think learning is important, so here's what this does. First we create a Rigidbody variable, and then we store the reference to the Rigidbody returned by the GetComponent we call on the GameObject returned by Instantiate(). We then call AddRelativeForce on that Rigidbody to get our projectile moving. We use the relative version on AddForce so that it moves according to the projectile's local rotation. We use Force$$anonymous$$ode.VelocityChange to instantly change its speed as apposed to slowly accelerating it. If these have helped you, please accept the reply as the answer. Good luck! :)
Your answer
Follow this Question
Related Questions
Slow automatic shooting script? 2 Answers
How to shoot a bullet and animate at the same time??? Its a 2D shooter c# 3 Answers
How do I stop a 2D projectile when it reaches a point 3 Answers
How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers
Shooting a bullet object toward the mouse position in a 2D. 1 Answer