I am current working on a kinda brawler-like game where 2 players shoot each other using one keyboard as controls. About 30 minutes ago I had the 2 players shooting bullets properly but for some reason, Player 2 no longer shoots bullets.Instead of shooting bullets, Player 2 kinda just creates a small invisible forward force when near him.I’ve been trying to find the source of the problem but I’ve failed for about 30 minutes now so I’m looking to see if someone may know how to fix this.
This is the script for player 2
using UnityEngine;
public class Player2Controller : MonoBehaviour {
public float P2speed;
public float P2JumpHeight;
public GameObject P2Bullet;
public GameObject P2Emitter;
public float P2Bullet_speed;
private Rigidbody P2rb;
private bool P2grounded;
private bool FacingLeft;
public int P2health;
void Start () {
P2rb = GetComponent<Rigidbody>();
P2grounded = true;
FacingLeft = true;
}
void Fire(){
GameObject P2Temporary_Bullet_Handler;
P2Temporary_Bullet_Handler = Instantiate(P2Bullet,P2Emitter.transform.position,P2Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = P2Temporary_Bullet_Handler.GetComponent<Rigidbody>();
if(FacingLeft == false){
Temporary_RigidBody.AddForce(new Vector3(1, 0, 0) * P2Bullet_speed);
}
if(FacingLeft == true){
Temporary_RigidBody.AddForce(new Vector3(-1, 0, 0) * P2Bullet_speed);
}
Destroy(P2Temporary_Bullet_Handler, 3f);
}
void FixedUpdate () {
if(Input.GetKey(KeyCode.RightArrow)){
P2rb.AddForce(new Vector3(10, 0, 0) * P2speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 180, 0);
FacingLeft = false;
}
if(Input.GetKey(KeyCode.LeftArrow)){
P2rb.AddForce(new Vector3(-10, 0, 0) * P2speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 0, 0);
FacingLeft=true;
}
if(Input.GetKey(KeyCode.UpArrow) && P2grounded == true){
Debug.Log("Jump");
P2rb.AddForce(new Vector3(0, 10, 0) * P2JumpHeight * Time.deltaTime);
P2grounded = false;
}
if(Input.GetKeyDown(KeyCode.RightControl)){
Debug.Log("Shoot2");
Fire();
}
if(P2health<= 0){
Destroy(gameObject);
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Ground")){
P2grounded = true;
Debug.Log("Grounded");
}
}
}
The 2 Scripts are basically the same [i think] with the only difference is that the script for the 2nd player has a “P2” prefixed to the variables to prevent complications.
This is the script for player1
using UnityEngine;
public class Player1Controller : MonoBehaviour {
public float speed;
public float JumpHeight;
public GameObject Bullet;
public GameObject Emitter;
public float Bullet_speed;
private Rigidbody rb;
private bool grounded;
private bool FacingLeft;
public int health;
void Start () {
rb = GetComponent<Rigidbody>();
grounded = true;
FacingLeft = false;
}
void Fire(){
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Emitter.transform.position,Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
if(FacingLeft == false){
Temporary_RigidBody.AddForce(new Vector3(1, 0, 0) * Bullet_speed);
}
if(FacingLeft == true){
Temporary_RigidBody.AddForce(new Vector3(-1, 0, 0) * Bullet_speed);
}
Destroy(Temporary_Bullet_Handler, 3f);
}
void FixedUpdate () {
if(Input.GetKey(KeyCode.D)){
rb.AddForce(new Vector3(10, 0, 0) * speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 0, 0);
FacingLeft = false;
}
if(Input.GetKey(KeyCode.A)){
rb.AddForce(new Vector3(-10, 0, 0) * speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 180, 0);
FacingLeft=true;
}
if(Input.GetKey(KeyCode.W) && grounded == true){
Debug.Log("Jump");
rb.AddForce(new Vector3(0, 10, 0) * JumpHeight * Time.deltaTime);
grounded = false;
}
if(Input.GetKeyDown(KeyCode.Space)){
Fire();
}
if(health<= 0){
Destroy(gameObject);
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Ground")){
grounded = true;
Debug.Log("Grounded");
}
}
}
Hopefully someone can help me