For some reason, the input system on a multiplayer 3d platformer I am making is not working. I looked at other solutions, but none of them worked. I used degub.log to try to see if the input is going through, but it is not registering. However, the player gameObject still reacts to keyboard input. It moves around like it is supposed to, but the jumping does not work and none of the Debug.log lines are being run, leading me to believe that the code is not receiving input... My code for the player is posted below
using UnityEngine;
using UnityEngine.Networking;
//using UnityEngine.SceneManagement; <--not used yet
public class LANPlayerMovement : NetworkBehaviour {
public Rigidbody rb;
public Camera cam;
public GameObject sceneCam;
public float speed = 10f;
public float jumpHeight = 7f;
public float jumpReductionFactor = 1.5f;
public float jumpTimeDelay = 0.3f;
public float currentHeight;
public int maxJumps = 2;
public int maxHealth = 10;
private int numJumps = 0;
private int attempts = 0;
private int currentHealth = 10;
private float currentJumpHeight;
private float currentJumpTime = 0f;
private float deathLevel = 5f;
private Vector3 startPosition;
private Vector3 startVelocity;
private NetworkStartPosition spawnPoint;
// Called at the beginning of program
private void Start()
{
if (isLocalPlayer) {
spawnPoint = FindObjectOfType();
cam.GetComponent().enabled = true;
} else {
cam.GetComponent().enabled = false;
}
startVelocity = new Vector3(0, 0, 0);
}
// Called by other scripts to damage the player
public void TakeDamage(int dmgAmount)
{
if (isLocalPlayer) {
currentHealth -= dmgAmount;
CheckDie();
}
}
// Update is called once per frame
void Update () {
if (isLocalPlayer) {
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetAxisRaw("Horizontal") == 1f)
{
Debug.Log("Right pressed!");
rb.AddForce(Vector3.right * speed, ForceMode.Acceleration);
}
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetAxisRaw("Horizontal") == -1f)
{
Debug.Log("Left pressed!");
rb.AddForce(Vector3.left * speed, ForceMode.Acceleration);
}
if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetAxisRaw("Vertical") == 1f) && numJumps < maxJumps && (Time.time - currentJumpTime) > jumpTimeDelay)
{
Debug.Log("Jump pressed!");
rb.AddForce(Vector3.up * currentJumpHeight, ForceMode.Impulse);
numJumps++;
currentJumpHeight = currentJumpHeight / jumpReductionFactor;
currentJumpTime = Time.time;
}
CheckDie();
}
}
// Called when collision occurs
private void OnCollisionEnter(Collision col)
{
if (isLocalPlayer)
{
currentHeight = transform.position.y;
if (col.gameObject.tag == "floor")
{
numJumps = 0;
currentJumpHeight = jumpHeight;
}
else if (col.gameObject.tag == "damage")
{
TakeDamage(1);
}
else if (col.gameObject.tag == "kill")
{
TakeDamage(currentHealth);
}
else if (col.gameObject.tag == "getTime")
{
PlayerPrefs.SetFloat("finishTime", Time.timeSinceLevelLoad);
}
}
}
// Check if the death conditions are met
private void CheckDie()
{
if (isLocalPlayer)
{
if (transform.position.y < deathLevel)
{
Die();
}
else if (currentHealth <= 0)
{
Die();
}
}
}
// Kills player
private void Die()
{
if (isLocalPlayer)
{
currentHealth = maxHealth;
rb.velocity = startVelocity;
//transform.position = startPosition;
transform.position = spawnPoint.transform.position;
attempts++;
}
}
}
Any idea on how to fix this?
↧