Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Feature Request How can I make this into Controller Binds

Discussion in 'Input System' started by FPSDesigner, Feb 29, 2024.

  1. FPSDesigner

    FPSDesigner

    Joined:
    Feb 8, 2024
    Posts:
    13
    I think that the title says it but I need to make this into controller binds. I am making a splitscreen game and I need to differentiate the Players. So how can I make them not move in unision. this is the code.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     [Header("Movement")]
    9.     public float moveSpeed;
    10.  
    11.     public float groundDrag;
    12.  
    13.     public float jumpForce;
    14.     public float jumpCooldown;
    15.     public float airMultiplier;
    16.     bool readyToJump;
    17.  
    18.     [Header("Keybinds")]
    19.     public KeyCode jumpKey = KeyCode.Space;
    20.  
    21.     [Header("Ground Check")]
    22.     public float playerHeight;
    23.     public LayerMask whatIsGround;
    24.     bool grounded;
    25.  
    26.     public Transform orientation;
    27.  
    28.     float horizontalInput;
    29.     float verticalInput;
    30.  
    31.     Vector3 moveDirection;
    32.  
    33.     Rigidbody rb;
    34.  
    35.     private void Start()
    36.     {
    37.         rb = GetComponent<Rigidbody>();
    38.         rb.freezeRotation = true;
    39.     }
    40.  
    41.     private void Update()
    42.     {
    43.         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
    44.  
    45.         MyInput();
    46.         SpeedControl();
    47.         ResetJump();
    48.  
    49.         if (grounded){
    50.           rb.drag = groundDrag;
    51.         }
    52.         else {
    53.           rb.drag = 0;
    54.         }
    55.     }
    56.  
    57.     private void FixedUpdate()
    58.     {
    59.         MovePlayer();
    60.     }
    61.  
    62.     private void MyInput()
    63.     {
    64.         horizontalInput = Input.GetAxisRaw("Horizontal");
    65.         verticalInput = Input.GetAxisRaw("Vertical");
    66.  
    67.         if(Input.GetKey(jumpKey) && readyToJump && grounded){
    68.             readyToJump = false;
    69.  
    70.             Jump();
    71.  
    72.             Invoke(nameof(ResetJump), jumpCooldown);
    73.         }
    74.     }
    75.  
    76.     private void MovePlayer()
    77.     {
    78.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    79.  
    80.         if(grounded){
    81.           rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    82.         }
    83.  
    84.         else if(!grounded)
    85.            rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    86.     }
    87.  
    88.     private void SpeedControl(){
    89.         Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    90.  
    91.         if(flatVel.magnitude > moveSpeed){
    92.             Vector3 limitedVel = flatVel.normalized * moveSpeed;
    93.             rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
    94.         }
    95.     }
    96.  
    97.     private void Jump(){
    98.         rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    99.  
    100.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    101.     }
    102.  
    103.     private void ResetJump(){
    104.         readyToJump = true;
    105.     }
    106. }
    107.  
     
  2. FPSDesigner

    FPSDesigner

    Joined:
    Feb 8, 2024
    Posts:
    13
    This is not my code it is from this guy
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,221
    If you look at the documentation for Keycode, there's options for joystick buttons: https://docs.unity3d.com/ScriptReference/KeyCode.html

    You can google guides as to which joystick button refers to a physical button on any given controller. And you can see there's options for joysticks 1 through to 8.

    Otherwise you can look at the new input system. Plenty of resources out there on how to use it, and how to make it work for multiple controllers.
     
  4. FPSDesigner

    FPSDesigner

    Joined:
    Feb 8, 2024
    Posts:
    13
    Thank you so much, hope you have a great week!