- Home /
left and right shooting
I am new to unity and I copy and pasted this code from somewhere else. I'm trying to create an offline 2 player shooting super smash bros type of game. With this code the player only shoot upwards I want to make it so if they are moving to the left it shoots left, same with right.and if the player stays still it shoots up. The game is 3d but its on a 2d map (Like super smash bros). Can you please modify the code to do this
public GameObject bulletPrefab;
public Transform bulletSpawn;
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Fire();
}
}
void Fire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
}
I also have 2 different shooting scripts for player 1 and 2 if that means anything
Comment
Best Answer
Answer by bhavinbhai2707 · Jan 24, 2018 at 08:35 PM
considering that players are already facing z direction and all rotations are 0
void Update()
{
//simply rotate left or right(write this code in your script that controls player
if (Input.GetKeyDown(KeyCode.A))
{
player.transform.Rotate(0,-90,0);
}
if (Input.GetKeyDown(KeyCode.D))
{
player.transform.Rotate(0,90,0);
}
}