Question by
Bradybeast128 · Sep 25, 2018 at 06:19 AM ·
controllergravitynot workinggolfco-op
Player 2 doesn't respond
I'm making a co-op / single player golf game with gravity physics and for some reason, player 1 works perfectly fine while the controller for player 2 doesn't work at all except for camera zoom. Here is both the camera controller and the player script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
public GolfBall golf; //script that is attached to the gameobject that allows the ball to move
public Camera cam; //The camera that is attached to the golf ball;
public bool Player2; // The same as player 2 in the GolfBall script, I just though it would help to have it be a local bool
void Start()
{
}
// Update is called once per frame
void Update () {
cam = GetComponentInChildren<Camera>();
golf = GetComponent<GolfBall>();
Player2 = golf.player2;
//The basic camera controls for player 1
if (Player2 == false)
{
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.down * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.left * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Rotate(Vector3.right * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Q))
{
transform.Rotate(Vector3.forward * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E))
{
transform.Rotate(Vector3.back * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.F))
{
cam.fieldOfView -= Time.deltaTime * 70;
}
if (Input.GetKey(KeyCode.R))
{
cam.fieldOfView += Time.deltaTime * 70;
}
}
//The basic camera controls for player 2
if (Player2 == true)
{
if (Input.GetKey(KeyCode.Keypad4))
{
transform.Rotate(Vector3.up * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Keypad6))
{
transform.Rotate(Vector3.down * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Keypad8))
{
transform.Rotate(Vector3.left * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Keypad5))
{
transform.Rotate(Vector3.right * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Keypad7))
{
transform.Rotate(Vector3.forward * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Keypad9))
{
transform.Rotate(Vector3.back * 70 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.KeypadEnter))
{
cam.fieldOfView -= Time.deltaTime * 70;
}
if (Input.GetKey(KeyCode.KeypadPlus))
{
cam.fieldOfView += Time.deltaTime * 70;
}
}
}
}
and the character controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GolfBall : MonoBehaviour {
public Slider power; //How much power is being forced onto the rigidbody
public GameObject slider; //The gameobject attached to the sider power
public bool inair; //If the gameobject has a collision with a planet, this is false
private Rigidbody r; // the rigidbod attached to this Gameobject
private Camera c; // the camera that is a child of this gameobject
public int score; // How many times the ball has been hit
public float cooldown; // the cooldown between swings
public bool player2; //Is this player 2?
public Text te; // the text that shows the player's score
public bool finished; // has this object hit the flag?
// Use this for initialization
void Start ()
{
slider = power.gameObject;
r = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (finished == true)
{
this.enabled = false;
// if this object has hit the flag, then disable this compontent so the player cant move
}
else
{
cooldown -= Time.deltaTime;
if (player2 == false)//is this player 2?
{
//The text shows the player score
te.text = "Player 1 : " + score;
if (Input.GetKey(KeyCode.X))
{
//if the player presses the x key, the power goes down
power.value -= Time.deltaTime * 1000;
}
if (Input.GetKey(KeyCode.Z))
{
//if the player presses the z key, the power goes up
power.value += Time.deltaTime * 1000;
}
}
if (player2 == true)//is this player 2?
{
//The text shows the player score
te.text = "Player 2 : " + score;
if (Input.GetKey(KeyCode.Keypad2))
{
//if the player presses the keypad 2, the power goes down
power.value -= Time.deltaTime * 1000;
}
if (Input.GetKey(KeyCode.Keypad1))
{
//if the player presses the keypad 1, the power goes up
power.value += Time.deltaTime * 1000;
}
}
if (inair == false) // is the current gameobject in air?
{
//if not, then the slider is active
slider.SetActive(true);
}
else
{
//if it is, than set the power slider to inactive and inair to true
slider.SetActive(false);
inair = true;
}
}
}
void Up()
{
if (cooldown <= 0) // if cooldown is less than zero
{
if (player2 == false) // is this player 2?
{
if (Input.GetKeyDown(KeyCode.Space))//if player 1 hit the spacebar, than add 1 to score, make cooldown equal 1, and add force to the rigidbody times the value of power slider
{
score += 1;
cooldown = 1;
inair = true;
r.AddForce(transform.forward * power.value);
}
else
{
r.AddForce(Vector3.zero);
}
}
if (player2 == true)
{
if (Input.GetKeyDown(KeyCode.RightShift))//if player 2 hits rightshift, than add 1 to score, make cooldown equal 1, and add force to the rigidbody times the value of power slider
{
score += 1;
cooldown = 1;
inair = true;
r.AddForce(transform.forward * power.value);
}
else
{
r.AddForce(Vector3.zero);
}
}
}
}
void OnCollisionStay(Collision other) // if it detects a collision
{
r.AddForce(Vector3.zero);
if (other.gameObject.tag == "Planet")//if the collision is with a planet, than inair equals false and use the up function
{
inair = false;
Up();
}
else
{
inair = true;
}
if (other.gameObject.tag == "Boundry")// WIP out of bounds
{
score += 3;
}
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Flag") //if the gameobject hits the flag, finished is true
{
finished = true;
}
if (other.gameObject.tag == "Atmosphere")//atmospheric drag
{
r.drag = 5;
}
if (other.gameObject.tag == "BlackHole")
{
//i dont know what im going to put here yet
}
}
}
Any help is appreciated. Thank you!
Comment