Solved
Problem with projectile direction
I have projectiles that the player can shoot. I have no problem instantiating them and applying the velocity for them to move. However, I cannot seem to get it to send the projectile in the opposite direction from whatever it defaults to.
For Example:
Player moving right -> shoots right Player moving left -> shoots left
I have code that should (in theory) handle it, but it doesn't seem to be listening to that code. I've checked to make sure the bool is switching and it's even hitting the break point to go into the code to make that line happen. I'm at a bit of a loss right now. I'm still working through it on my own, but would appreciate input.
//Handles attacking
if (Input.GetKeyDown(KeyCode.Space))
{
//Handle Switching Projectiles/Attacks
switch (attackNumber)
{
case 1:
if (movingRight)
{
Rigidbody rightShot = Instantiate(fireProjectile, transform.position + (transform.forward * 0.5f), transform.rotation) as Rigidbody;
rightShot.velocity = new Vector3(bulletSpeed * Time.deltaTime, 0, 0);
}
else if (!movingRight)
{
Rigidbody leftShot = Instantiate(fireProjectile, transform.position + (transform.forward * 0.5f), transform.rotation) as Rigidbody;
leftShot.velocity = new Vector3(-bulletSpeed * Time.deltaTime, 0, 0);
}
break;
case 2:
playerMove.jumpForce = 600;
//playerMove.jumpForce = 600;
//magicUses--;
//Debug.Log(magicUses);
break;
}
}
I'm sure there is probably a better way to go about this, but this was the first thing I thought of to handle it. Any input would be greatly appreciated! Once I fix this, I can apply the same concept to my enemy characters to make them shoot the right direction too! :D
Hi @Paul2357
I didn't quite understand the problem. So you want the bullet to go to the right when you are moving to the right and to the left when moving left ? Also Where is the bullet going right now ?
@baltazard Yes I'm trying to get projectiles to fire in the direction the player is facing. Right now it travels right no matter what
Try maybe this : leftShot.velocity = transform.forward * bulletSpeed * Time.deltaTime