- Home /
Question by
Marijenburg · Jan 17, 2020 at 09:24 PM ·
inputcontrollerplayer movement
How can I translate my arrow keys input to my controller joystick?
Here is my player movement script, how can I make this movement be done by controller joystick as well?
using UnityEngine;
public class PlayerMovement : MonoBehaviour { //On crée un slot pour le rigidbody, on l'appelle rb puis dans unity on le glisse dedans
public LayerMask groundLayers;
public Rigidbody rb;
public float forwardForce = 40f;
public float sidewaysForce = 25f;
public float forwardInair = 300f;
public float sidewaysInair = 300f;
public float jumpForce = 12f;
public SphereCollider col;
public float sideForceController = 2f;
public float sideAirForceController = 1f;
// Update is called once per frame
void FixedUpdate()
{
Cursor.visible = false;
// the force that pushes straight forward
if (IsGrounded())
{
rb.AddForce(0, 0, forwardForce, ForceMode.Force);
}
else
{
rb.AddForce(0, 0, forwardInair, ForceMode.Force);
}
//go right
if (IsGrounded())
{
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0,ForceMode.Impulse);
}
}
else
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(sidewaysInair * Time.deltaTime, 0, 0, ForceMode.Acceleration);
}
//go left
if (IsGrounded())
{
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.Impulse);
}
}
else
{
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.AddForce(-sidewaysInair * Time.deltaTime, 0, 0, ForceMode.Acceleration);
}
}
//jump
if (IsGrounded() && Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
//jump with controller
if (IsGrounded() && Input.GetKey(KeyCode.Joystick1Button0))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
if (rb.position.y < -10f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
//to check if grounded
private bool IsGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x,
col.bounds.min.y, col.bounds.center.z), col.radius * .9f, groundLayers);
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I disable player input action map and enable the UI action map? 4 Answers
Disabling Player Movement when inputting text? 0 Answers
Vive Controller not enabled until mouse click 0 Answers
How do I standardize my game's input to different types of controllers? 1 Answer
VR "Hold" Screen for gallery Instal 0 Answers