Search Unity

Question Moving character over a rotating platform.

Discussion in 'Scripting' started by Zenithin, Sep 9, 2020.

  1. Zenithin

    Zenithin

    Joined:
    Jun 7, 2016
    Posts:
    35
    So I have been pretty stuck on how to achieve this effect as in fall guys.

    The platform rotates.
    The character rotates with the platform but can move over it and jump also.

    Do I make both of them rigidbodies or not?
    Currently my character stays stuck at one position or moves very slowly as compared to rotation of the platform.
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    How about a new Script called "RotatingPlattform" which have an event,
    whenever any GameObject steps on it, it turns with the Plattform itself
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Try making the player transform a child of said moving object.
     
  4. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    128
    Not sure if this is what your looking for. When player falls on the rotating platform with the nametag platform
    it moves with it.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThePlayer : MonoBehaviour
    6. {
    7.  
    8.  
    9.     private void OnCollisionEnter(Collision collision)
    10.     {
    11.         if (collision.gameObject.CompareTag("Platform"))
    12.         {
    13.  
    14.             this.transform.parent = collision.transform;
    15.  
    16.         }
    17.      
    18.     }
    19.     private void OnCollisionExit(Collision collision)
    20.     {
    21.         if (collision.gameObject.CompareTag("Platform"))
    22.             this.transform.parent = null;
    23.     }
    24. }
    25.  
    26.  
    27.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rotateplatform : MonoBehaviour
    6. {
    7.     public float rotatingSpeed = 10f;
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         transform.Rotate(Vector3.up, rotatingSpeed * Time.deltaTime);
    20.        
    21.     }
    22. }
    23.  
     
  5. gabagpereira

    gabagpereira

    Joined:
    Jan 21, 2019
    Posts:
    24
    Ignore this if you already managed to solve the problem, but I was just looking for the same until I remembered the LEGO Microgame implements that mechanic, so I used their MinifigController script as a guide and was able to successfully reproduce what you intend.

    Basically you need 2 methods on the script that controls your character:
    • OnControllerColliderHit (assuming you are using a CharacterController): here you'll perform a ShpereCast, when the player is grounded, to get the collider that's underneath them and store it's transform inside a variable.
    • Update: before applying any movement and rotation, you need to calculate and add the values for external motion and rotation based on the previously stored transform.
    I confess I would never be able to achieve this by my own means, but it's really not hard to figure it out if you use the mentioned script as a guide. So, install the LEGO Microgame and check it out.
     
    al_konst likes this.