Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Divive · Nov 27, 2016 at 02:56 PM · c#move an objectplatfrom

How do I make objects move with the moving platform?

I'm very new to Unity and trying to make a 2d platforming game where you solve puzzles using crates and moving platforms. I have made a platform that moves, however when the player or any object with a rigidbody is on the platform it won't move with it and just stays in place.

This is my code for the moving platform:

 using UnityEngine;
 using System.Collections;
 
 public class PlatformMovement : MonoBehaviour {
 
     public bool activated;
 
     public GameObject player;
 
     private Vector3 posA;
     private Vector3 posB;
     private Vector3 nextPos;
 
     [SerializeField]
     private float speed;
 
     [SerializeField]
     private Transform childTransform; 
 
     [SerializeField]
     private Transform transformB;    
 
 
 
     // Use this for initialization
     void Start () {
 
         posA = childTransform.localPosition;
         posB = transformB.localPosition;
         nextPos = posB;
 
 
     }
     
 
 
 
     // Update is called once per frame
     void Update () {
 
         Move();
         
 
     }
 
 
     public void OnTriggerStay2D(Collider2D other)
     {
 
         Transform thingOnPlatform = other.transform;
         thingOnPlatform.position = Vector3.MoveTowards(thingOnPlatform.position, nextPos, (speed * Time.deltaTime));
 
     }
 
 
     private void Move()
     {
         childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nextPos, speed * Time.deltaTime); 
 
         if (Vector3.Distance(childTransform.localPosition,nextPos) <= 0.1) 
         {
             ChangeDir();
         }
     }
 
     private void ChangeDir()
     {
         nextPos = nextPos != posA ? posA : posB; 
 
     }
 }
 





I'm using Unity 5.4.0f3 if you need to know.

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 Adam-Mechtley · Nov 28, 2016 at 09:26 AM

Hi! Right now you are directly setting the transform's localPosition. I'm not sure if it will solve the problem in your case, but you might instead consider using Rigidbody2D.SetPosition (). (Likewise, make sure your moving platform has a Rigidbody2D component with the isKinematic flag to set to true.)

Comment
Add comment · Show 2 · 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
avatar image Divive · Dec 02, 2016 at 08:38 AM 0
Share

I tried this:

     public void OnTriggerStay2D(Collider2D other)
     {
 
         Rigidbody2D thingOnPlatform = other.gameObject.GetComponent<Rigidbody2D>();
         thingOnPlatform.$$anonymous$$ovePosition(thingOnPlatform.position + GetComponent<Rigidbody2D>().velocity  *Time.fixedDeltaTime);
 
     }



I don't know if i did it wrong (I'm very new to unity and program$$anonymous$$g in general) but it didn't work. Ins$$anonymous$$d my character couldn't move at all.

avatar image Adam-Mechtley Divive · Dec 02, 2016 at 01:58 PM 0
Share

Sorry for the lack of clarity on my part. I meant that you should be moving the platform using Rigidbody2D.$$anonymous$$ovePosition () inside your $$anonymous$$ove () function. I'm not sure exactly what you're trying to do inside of OnTriggerStay2D () though. If your platform has a collider and a kinematic Rigidbody2D, then any non-kinematic Rigidbody2D with a collider sitting on top of it should automatically be moved around via physics.

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

7 People are following this question.

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

Related Questions

Move Object in Button Click smooth to position? 2 Answers

Click on object shows menu, but also move it. 0 Answers

Moving moving Object with left/right arrow keys in a circular direction 3 Answers

Why the loop in waypoints in not working when set to true? 0 Answers

How do I move a game object only on the x axis with arrow keys? 1 Answer


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