Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to move player automatically while standing on a moving surface?

Discussion in 'Scripting' started by androidac67, Jul 18, 2023.

  1. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    I have a standard isGrounded check for my Unity3D character controller but I cannot figure out how to move my player automatically while standing on an object/surface that is moving. For example if I have a car object that is moving in a straight line on a loop and my player stands on the roof of the car, the player is supposed to move with the car by default, unless I intervene by moving my player myself.

    I have code below that I have on my rotating object, but on runtime my player flies around the scene. I've tried using both Transform & simply initializing a public GameObject Player but neither helped solve the problem. But according to the docs for Quaternion.Inverse, it seems to fit the description of what I need but I still can't figure it out.

    Code (CSharp):
    1. // WheelRotate object script
    2.  
    3. void PlayerMoveAfterRotation(Quaternion r)
    4. {
    5.     Quaternion q = transform.rotation * r * Quaternion.Inverse(transform.rotation);
    6.     player.transform.position = transform.position + q * (player.transform.position - transform.position);
    7. }
    8.  
    9. void Update()
    10. {
    11.     transform.Rotate(RotateAmount * speed * Time.deltaTime); // rotate the object
    12.  
    13.     PlayerMoveAfterRotation(Quaternion.AngleAxis(Time.deltaTime * 30, Vector3.right));
    14. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    There's a bunch of ways to do it depending on how you're moving the platform, physics, etc.

    I would start with some Youtube tutorials, searching with key terms that help narrow down how you're doing stuff.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  3. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    There actually doesn't seem to be many tutorials at all about this unless I'm looking in the wrong places. I've gone through quite a number of videos on youtube but there's never been any references to ground checks on moving surfaces.

    In terms of the way my platforms are moving, I'm keeping everything simple, whether it be with a standard animation or a simple line of code to rotate the object (like above). My rotating object has no rigidbody attached and is a simple capsule object with a capsule collider. I have also tried using a rigidbody with is Kinematic on and also a mesh collider + renderer, but all the results were the same.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I respect that. Seriously, I do.

    So I just went and made the simplest platform stand-on-and-follow ever.

    Works in 2D, 3D, whatever. The script:

    https://gist.github.com/kurtdekker/5d8ae36fcd1455b8c9a5ef3913762227

    Super small codelet, super simple setup.

    I just now set it up on the BasicFPCC character controller in about 3 minutes. It was INSANELY easy to do.

    Here's the controller I modified to use the PlatformMotionRecorder above:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!

    I'm honestly a little disappointed in myself for not realizing until now how simple it could be in Unity with this approach. It's like a total of 5 lines of code everywhere, maybe 10.
     
    Chubzdoomer likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Like Kurt said it depends on how you're moving your player. If the player is moving (correctly) via a rigidbody, your platforms need to be moved via the rigidbody API too so the colliders interact properly.

    If you're not moving via rigidbodies, you will need to keep track of the motion of the objects below the player and apply that motion back in kind. This might also be required still with rigidbodies, depending on the method of which your character is being moved.
     
  6. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    Hey sorry I had school and couldn't reply. I don't have any knowledge of MotionRecorder but I created the script in your github and I attached it to a moving surface object, and I followed the rest of the instructions in the github. However the one part I am unfamiliar with is: " get its .Movement property and add it to your own
    // movement amount this frame."
    I know how to get the movement property from your script, but I am not sure how to use that to adjust the speed of my controller.

    Code (CSharp):
    1. void LateUpdate() // inside my PlayerMovement script
    2. {
    3.     if (isGrounded)
    4.     {  
    5.         if (Physics.Raycast(transform.position, -transform.up, 1.5f))
    6.         {
    7.             PlatformMotionRecorder motion GetComponent<PlatformMotionRecorder>()
    8.         }
    9.     }
    10. }
    11.  
    My movement code is the standard Unity docs character controller code, I just use:

    Code (CSharp):
    1. x = Input.GetAxis("Horizontal");
    2. z = Input.GetAxis("Vertical");
    3.  
    4. Vector3 move = transform.forward * z + transform.right * x;
    5.  
    6. move *= speed;
    7. velocity.y += gravity * Time.deltaTime; // Gravity for jumps
    8. controller.Move(velocity * Time.deltaTime);
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    See line 8 above?

    Pull it apart.

    - multiply
    velocity
    by Time.deltaTime and store it back in velocity

    - add in the
    .Movement
    you get from the platform you may be standing in

    - use velocity in the call to
    controller.Move()
     
  8. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    Yeah so inside the LateUpdate I put the following as putting it in my Update() caused the controller to not move:

    velocity = motions.Movement * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);


    So basically I am adjusting my velocity by including the Movement component of the PlatformMotionRecorder script and then multiplying it by Time.deltaTime, and then I use that velocity to move my controller. However when I stand on a surface that is moving left and right (no rotations) and has the MotionRecorder script attached, my player doesn't seem to follow the surface position. I'm not quite seeing what I might be missing.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    That's not adding.

    That's replacing.

    Read the code notes again, as well as my post above.

    You also should ONLY do that if motions is non-null. That's ALSO called out in the notes but is obvious because of how C# works with references.