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 ErraticVelocity · Aug 15, 2016 at 06:06 PM · 2d-platformerrigidbody2dphysics2dmoving platform

2d Moving Platforms

okay so I know this has been asked before but it seems like the other implementations are different from my own. Im working on a basic platformer and im having some issues with moving platforms. I've tried parenting, and im currently playing with directly setting the velocity when they are touched. I added a second collider to my player so that it will only check on his bottom but i cant get the velocity to transfer correctly.

Here is how my player object is set-up alt text

and heres my platform

alt text

but when it runs depending on which implementation i use one of two things happens.

  1. if i parent then the player remains completely stationary

  2. if i set velocity the player moves at a different speed from the platform, but the velocity shows up as being the same between the two.

here is my code so far.

character controller

 using UnityEngine;
 using System.Collections;
 
 public class characterController : MonoBehaviour {
     //axis values
     public float horizontal = 0;
     public float vertical = 0;
     public float shoot = 0;
     public float moveMultiplier = 1;
     public float jumpTime = 3;
     public float jumpSpeed = 1;
     public float jumpCounter = 0;
     public bool jumping = false;
     Rigidbody2D phy;
     // Use this for initialization
     void Start ()
     {
         phy = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         //Debug.Log("player Velocity: " + Time.fixedTime + " - " + GetComponent<Rigidbody2D>().velocity);
         horizontal =Input.GetAxis("horizontal");
         vertical = Input.GetAxis("vertical");
 
         //movement
         phy.velocity = new Vector2((horizontal * moveMultiplier), phy.velocity.y);
 
         //jumping
         if (!jumping)
         {
             if (vertical != 0 && jumpCounter<jumpTime)
             {
                 phy.velocity = new Vector2(phy.velocity.x, jumpSpeed * vertical);
                 jumping = true;
             }
         }
         else
         {
             if (vertical != 0 && jumpCounter < jumpTime)
             {
                 phy.velocity = new Vector2(phy.velocity.x, jumpSpeed * vertical);
             }
             jumpCounter += Time.deltaTime;
         }
         if(phy.velocity.y==0)
         {
             jumping = false;
             jumpCounter = 0;
         }
     }
 }

Platform code

 using UnityEngine;
 using System.Collections;
 
 public class velMovingPlatform : MonoBehaviour {
     public GameObject startPoint;
     public GameObject endPoint;
     public Vector2 distance;
     public float time=5;
     public Vector2 velocityReq;
     public int phase = 0;
     public float waitTime = 3;
     public float timer=0;
 
     // Use this for initialization
     void Start ()
     {
         Vector2 startPos = startPoint.transform.position;
         Vector2 endPos = endPoint.transform.position;
         distance = new Vector2(Mathf.Abs(startPos.x - endPos.x), Mathf.Abs(startPos.y - endPos.y));
         velocityReq = new Vector2(distance.x / time, distance.y / time);
 
     }
     
     // Update is called once per frame
     void Update ()
     {
         //log player velocity
         //Debug.Log("platform Velocity: " + Time.fixedTime + " - " + GetComponent<Rigidbody2D>().velocity);
         switch (phase)
         {
             case 0:
                 GetComponent<Rigidbody2D>().velocity = Vector2.zero;
                 timer += Time.deltaTime;
                 timer = Mathf.Clamp(timer, 0, waitTime);
                 if(timer==waitTime)
                 {
                     phase = 1;
                     timer = 0;
                 }
                 break;
             case 1:
                 timer += Time.deltaTime;
                 timer = Mathf.Clamp(timer, 0, time);
                 if (timer == time)
                 {
                     phase = 2;
                     timer = 0;
                 }
                 else
                 {
                     GetComponent<Rigidbody2D>().velocity = velocityReq;
                 }
                 break;
             case 2:
                 GetComponent<Rigidbody2D>().velocity = Vector2.zero;
                 timer += Time.deltaTime;
                 timer = Mathf.Clamp(timer, 0, waitTime);
                 if (timer == waitTime)
                 {
                     phase = 3;
                     timer = 0;
                 }
                 break;
             case 3:
                 timer += Time.deltaTime;
                 timer = Mathf.Clamp(timer, 0, time);
                 if (timer == time)
                 {
                     phase = 0;
                     timer = 0;
                 }
                 else
                 {
                     GetComponent<Rigidbody2D>().velocity = -velocityReq;
                 }
                 break;
 
         }
     }
 }
 

Platform Controller

 using UnityEngine;
 using System.Collections;
 
 public class movingPlatformController : MonoBehaviour {
     public GameObject player;
     public bool locked = false;
     public Vector3 oldpos;
     public Vector3 newpos;
     public Vector3 difference;
     public GameObject othercollider;
 
     void OnTriggerEnter2D(Collider2D other)
     {
         othercollider = other.gameObject;
         //Debug.Log("colEnter layer: " + other.gameObject.layer);
         if (other.gameObject.layer == 8)
         {
            // player.transform.SetParent(other.transform);
         }
     }
 
     void OnTriggerStay2D(Collider2D other)
     {
         if(other.gameObject.layer==8)
         {
            
                 player.GetComponent<Rigidbody2D>().velocity = other.GetComponent<Rigidbody2D>().velocity;
                 Debug.Log(player.GetComponent<Rigidbody2D>().velocity + " / " + player.GetComponentInParent<Rigidbody2D>().velocity);
             
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         if (other.gameObject.layer == 8)
         {
            // player.transform.parent = null;
             locked = false;
         }
     }
 }
 

For the life of me i cant figure it out. Any Ideas?

screenshot-11.png (205.5 kB)
screenshot-12.png (205.1 kB)
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 oren_danniel · Sep 12, 2016 at 07:47 PM

First, parenting a rigidbody can cause some problems. now I know how to work around the parenting using a script that will give you the same affect without parenting the object, if i understand correctly you want the object to follow the stage?

the script will ensure that the object on the stage will stay at the same place on the stage as the it moves.

any how this is the script:

  public class rigidMoveStage : MonoBehaviour { public float Xdistance; public float Ydistance; public Vector2 vector; public Vector2 stageVector; public bool colBool = false;
  
       //this code needs to be on the colliding object
       //do let me know if there is any problem
       
       
       // Update is called once per frame
       void Update () {
           GameObject colObject = GameObject.Find("col");
           if (colBool == true) {
               //without gravity(locks y axis)
               this.gameObject.transform.position = new Vector2(stageVector.x + Xdistance, stageVector.y + Ydistance);
               
               
               //with gravity(unlocks y axis)
               //this.gameObject.transform.position = new Vector2(stageVector.x + Xdistance, this.gameObject.transform.position.y);            
               stageVector = colObject.gameObject.transform.position;
           }
       }
       void OnCollisionEnter2D(Collision2D col) {
           vector = transform.position;
           stageVector = col.gameObject.transform.position;
           col.gameObject.name = "col";
           Xdistance = vector.x - stageVector.x;
           Ydistance = vector.y - stageVector.y;
           colBool = true;
           //feel free to use it
           
       }
  
  } 



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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

(2D) Rigidbody slides a bit through Box Collider 1 Answer

FixedUpdate or Update for collision detection and climbing slopes? 1 Answer

Rigidbody (player) moves slower on moving platform 2 Answers

No overload for method. (Question) 0 Answers

How to move a Rigidbody2D along another Rigidbody2D 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