Question by
TreBomb004 · Nov 27, 2016 at 09:23 PM ·
buttoncontrollerconsolexboxcontrollerinput.getbutton
Why doesn't my script work with controllers?
So I have a script that makes a player jump, and I am trying to make it so that it works for both PC and Console. Right now however, the script is not working on my controller. Here is what I have:
using UnityEngine; using System.Collections;
public class Jump : MonoBehaviour {
private CharacterController controller;
private float verticalVelocity;
private float gravity = 20.0f;
private float jumpForce = 10f;
void Start () {
controller = GetComponent<CharacterController> ();
}
void Update () {
if (controller.isGrounded) {
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space)) {
verticalVelocity = jumpForce;
}
}
else {
if (Input.GetButtonDown (KeyCode.JoystickButton0)) {
verticalVelocity = jumpForce;
}
else {
verticalVelocity -= gravity * Time.deltaTime;
}
}
Vector3 moveVector = new Vector3 (0, verticalVelocity, 0);
controller.Move (moveVector * Time.deltaTime);
}
}
Comment
Answer by ElijahShadbolt · Nov 28, 2016 at 12:49 AM
using UnityEngine;
public class Jump : MonoBehaviour {
private CharacterController controller;
private float verticalVelocity;
private float gravity = 20.0f;
private float jumpForce = 10f;
void Start() {
controller = GetComponent<CharacterController> ();
}
void Update() {
if (controller.isGrounded) {
verticalVelocity = 0f;
if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.Joystick1Button0)) {
verticalVelocity += jumpForce;
}
}
verticalVelocity -= gravity * Time.deltaTime;
Vector3 moveVector = new Vector3(0, verticalVelocity, 0);
controller.Move(moveVector * Time.deltaTime);
}
}