Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by InfinityCubed · Feb 25, 2021 at 03:39 PM · physicsplayergameplaymoving platform

How to make a character move with a moving platform? (3D)

Hello everyone! I need to make a player - character controller - move on a moving platform, that has a box collider. I tried to make the player a child of the platform when on it but it doesnt seem to work. How can I implement this feature into my moving platform's script?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class movingPlatform : MonoBehaviour
 {
 
     [SerializeField]
     float speed;
 
     [SerializeField]
     Transform startPoint, endPoint;
 
     [SerializeField]
     float changeDirectionDelay;
 
     
     private Transform destinationTarget, departTarget;
 
     private float startTime;
 
     private float journeyLength;
 
     bool isWaiting;
 
 
     
     void Start()
     {
         departTarget = startPoint;
         destinationTarget = endPoint;
 
         startTime = Time.time;
         journeyLength = Vector3.Distance(departTarget.position, destinationTarget.position);
     }
 
    
     void Update()
     {
         Move();
     }
 
     private void Move()
     {
 
 
         if (!isWaiting)
         {
             if(Vector3.Distance(transform.position, destinationTarget.position) > 0.01f)
             {
                 float distCovered = (Time.time - startTime) * speed;
 
                 float fractionOfJourney = distCovered / journeyLength;
 
                 transform.position = Vector3.Lerp(departTarget.position, destinationTarget.position, fractionOfJourney);
             }
             else
             {
                 isWaiting = true;
                 StartCoroutine(changeDelay());
             }
         }
 
 
     }
 
     void ChangeDestination()
     {
 
         if(departTarget == endPoint && destinationTarget == startPoint)
         {
             departTarget = startPoint;
             destinationTarget = endPoint;
         }
         else
         {
             departTarget = endPoint;
             destinationTarget = startPoint; 
         }
 
     }
     IEnumerator changeDelay()
     {
         yield return new WaitForSeconds(changeDirectionDelay);
         ChangeDestination();
         startTime = Time.time;
         journeyLength = Vector3.Distance(departTarget.position, destinationTarget.position);
         isWaiting = false;
     }
 
 
 
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             other.transform.parent = transform;
 
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             other.transform.parent = null;
         }
     }
 
 }

I know this is a frequently asked topic and I'm sorry for that, but I do not know how to make this work. any help's welcome.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexc847 · Feb 27, 2021 at 03:41 AM

I'm not going to pretend that I am super good at Unity, and I don't know much about this myself. However, I will provide links that you could explore: https://answers.unity.com/questions/12083/how-to-get-a-character-to-move-with-a-moving-platf.html https://www.youtube.com/watch?v=rO19dA2jksk

Also, my own idea: maybe do something along the lines of parenting the player to the moving platform? Or create a trigger collider and set the player position to the moving platform's whenever the trigger collider detects the player?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can a player push an object? 5 Answers

Non slippery movement 1 Answer

Gravity and rotation control 0 Answers

AddExplosionForce 0 Answers

Keeping player stationary and let physics affect... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges