Search Unity

Question Moving a character controller on a moving platform.

Discussion in 'Scripting' started by Quetz242, Jun 5, 2023.

  1. Quetz242

    Quetz242

    Joined:
    Feb 5, 2021
    Posts:
    1
    Currently in my project I have a moving platform that the player must stay on. The platform is a rigidbody and the player is a character controller. Currently when the player lands on the platform it just sits there in space while the platform moves out from under them. I have tried making the player a child of the platform when they collide, but the player still stays motionless.

    Player movement script below.
    Code (CSharp):
    1.     public float walkSpeed = 12.0f;
    2.     public float runSpeed = 6.0f;
    3.     public float mouseSensitivity = 3f;
    4.     public float gravity = -9.81f;
    5.  
    6.     private float moveSpeed;
    7.     private CharacterController controller;
    8.     private Camera playerCamera;
    9.     private float verticalRotation = 0f;
    10.     private Vector3 velocity;
    11.  
    12.     void Start()
    13.     {
    14.         controller = GetComponent<CharacterController>();
    15.         playerCamera = GetComponentInChildren<Camera>();
    16.  
    17.         // Hide and lock the cursor to the center of the screen
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.         Cursor.visible = false;
    20.  
    21.         moveSpeed = walkSpeed;
    22.  
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         movement();
    28.         mouseLook();
    29.     }
    30.  
    31.     void movement()
    32.     {
    33.         if (Input.GetKey(KeyCode.LeftShift))
    34.             moveSpeed = runSpeed;
    35.         else
    36.             moveSpeed = walkSpeed;
    37.         // Player Movement
    38.         Vector3 moveDirection = transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical");
    39.         velocity = new Vector3(moveDirection.x * moveSpeed, gravity, moveDirection.z * moveSpeed);
    40.        
    41.         controller.Move(velocity * Time.deltaTime); ;
    42.     }
    43.  
    44.     void mouseLook()
    45.     {
    46.         // Player Rotation
    47.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
    48.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
    49.  
    50.         verticalRotation -= mouseY;
    51.         verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f);
    52.  
    53.         playerCamera.transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
    54.         transform.Rotate(Vector3.up * mouseX);
    55.     }
    56.  
    57.     private void OnControllerColliderHit(ControllerColliderHit hit)
    58.     {
    59.         transform.parent = hit.transform;
    60.         Debug.Log("Collided With" + hit.gameObject.name);
    61.     }
    I haven't used this site for help before btw so if you need more info to help just ask.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    No sense considering just the script. it's all extremely tightly coupled to the colliders and rigidbodies involved, and also the approach. Some approaches use some kind of a joint to attach the player while on the platform.

    I would start with some tutorials on the matter. Since you are mixing CharacterController and Rigidbody you have a bit harder row to hoe because you will be responsible for marshaling motion between the two of them, as they live in different worlds of the engine (see docs for more).

    Enclosed is an example of an arbitrary 2D solution.
     

    Attached Files: