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 /
This question was closed Nov 20, 2014 at 01:32 AM by meat5000 for the following reason:

Duplicate Question

avatar image
0
Question by Paromi · Sep 03, 2014 at 02:53 PM · character controllerplatformmoving platform

Can´t get the character to Stay on a moving Platform

I Know that this Question have been asked alot of times... I have searched the web and tryed everything i could find on this topic, but still i cant get it working.

here are my Specs:

Character:

BoxCollider2D, Not set as an Trigger

RigidBody2d, is not kinematic

c# Code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speedForce = 25f;
     public Vector2 jumpVector;
     public bool isGrounded;
     public float playerSpeed;
     public bool goesWest;
     public float minSwipeDistY;
 
 
     public Transform grounder;
     public float radiuss;
     public LayerMask ground;
 
     Animator anim;
 
     //
     void OnCollisionStay ( Collision hit) { 
                 if (hit.gameObject.tag == "platform") {
                         transform.parent = hit.transform; 
                 } else {
                         transform.parent = null;
                 }
         }
 
 
     void Start () {
         anim = GetComponent<Animator> ();
 
 
     }
     public bool get_playerDirection(){
         return goesWest;
     }
     // Update is called once per frame
     void Update () {
 
         // Shoot
 
         for (int i = 0; i < Input.touchCount; i++)
         {
             Touch touch = Input.GetTouch(i);
             
             // -- Tap: quick touch & release
             // ------------------------------------------------
             if (touch.phase == TouchPhase.Ended && touch.tapCount == 1)
             {
 
 
             
             {
                 WeaponScript weapon = GetComponent<WeaponScript>();
                 if (weapon != null)
                 {
                     // false because the player is not an enemy
                     weapon.Attack(false);
                 }
             }
 
             }
             }
         // Shoot end
 
         // Android Controll
 
         if (Application.platform == RuntimePlatform.Android) {
                         //    if (Input.GetKey (KeyCode.A)) {
                         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.x < 0) {
 
                                 playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
                 goesWest =true;
                                 rigidbody2D.velocity = new Vector2 (-playerSpeed, rigidbody2D.velocity.y);
                                 transform.localScale = new Vector3 (-0.1f, 0.1f, 1);
                                 anim.SetFloat ("Speed", Mathf.Abs (speedForce));
 
 
                                 //    } else if (Input.GetKey (KeyCode.D)) {
 
                         } else if (Input.GetTouch (0).deltaPosition.x > 0) {
             
                                 playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
                 goesWest =false;
                                 rigidbody2D.velocity = new Vector2 (playerSpeed, rigidbody2D.velocity.y);
                                 transform.localScale = new Vector3 (0.1f, 0.1f, 1);
                                 anim.SetFloat ("Speed", Mathf.Abs (speedForce));    
             
                         } else if (isGrounded) {        
                         
                                 rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
                                 anim.SetFloat ("Speed", Mathf.Abs (0));
                         }
 
                         isGrounded = Physics2D.OverlapCircle (grounder.transform.position, radiuss, ground);
 
 
                         //    if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
 
                         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.y > 0 && isGrounded == true) {
                                 rigidbody2D.AddForce (jumpVector, ForceMode2D.Force);
 
 
 
 
             
                         }
                 } else 
         
         // Pc Controlle///////////////////////////////////////////////////////////
         
             
                     {
         {
 
             // ...
             
             // 5 - Shooting
             bool shoot = Input.GetKeyDown(KeyCode.W);
             shoot |= Input.GetKeyDown(KeyCode.W);
             // Careful: For Mac users, ctrl + arrow is a bad idea
             
             if (shoot)
             {
                 WeaponScript weapon = GetComponent<WeaponScript>();
                 if (weapon != null)
                 {
                     // false because the player is not an enemy
                     weapon.Attack(false);
                 }
             }
 
             //
 
             if (Input.GetKey (KeyCode.A)) {
         //    if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.x < 0) {
                 
                 playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
                     goesWest =true;
                 rigidbody2D.velocity = new Vector2 (-playerSpeed, rigidbody2D.velocity.y);
                 transform.localScale = new Vector3 (-0.1f, 0.1f, 1);
                 anim.SetFloat ("Speed", Mathf.Abs (speedForce));
                 
                 
                 } else if (Input.GetKey (KeyCode.D)) {
                 
             //} else if (Input.GetTouch (0).deltaPosition.x > 0) {
                 
                 playerSpeed = isGrounded ? speedForce : speedForce * 0.7f;
                     goesWest =false;
                 rigidbody2D.velocity = new Vector2 (playerSpeed, rigidbody2D.velocity.y);
                 transform.localScale = new Vector3 (0.1f, 0.1f, 1);
                 anim.SetFloat ("Speed", Mathf.Abs (speedForce));    
                 
             } else if (isGrounded) {        
                 
                 rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
                 anim.SetFloat ("Speed", Mathf.Abs (0));
             }
             
             isGrounded = Physics2D.OverlapCircle (grounder.transform.position, radiuss, ground);
             
             
                 if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
             
             //if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (0).deltaPosition.y > 0 && isGrounded == true) {
                 rigidbody2D.AddForce (jumpVector, ForceMode2D.Force);
                 
             }
             
             
             
             }
             
         }
     }
     
         void OnDrawGizmos()
     {
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere (grounder.transform.position, radiuss);
 
         }
         
         void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "fire") {
             Debug.Log ("You are DEAD");
                        Application.LoadLevel (1);
         }
 
 
 
 
 
 
         }
     }

MovingPlatform:

BoxColider2D, is not an trigger

RigidBody2d , is kinematic

c# Code:

 using UnityEngine;
 using System.Collections;
 
 public class MovingPlatform : MonoBehaviour
 {
     private float useSpeed;
     public float directionSpeed = 9.0f;
     float origY;
     public float distance = 10.0f;
     
 
     void Start ()
     {
         origY = transform.position.x;
         useSpeed = -directionSpeed;
     }
     
 
     void Update ()
     {
         if(origY - transform.position.x > distance)
         {
             useSpeed = directionSpeed; //flip direction
         }
         else if(origY - transform.position.x < -distance)
         {
             useSpeed = -directionSpeed; //flip direction
         }
         transform.Translate(useSpeed*Time.deltaTime,0,0);
     }
 }

Comment
Add comment · Show 2
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 Malfegor · Sep 03, 2014 at 08:07 PM 0
Share

Right, for the moving platform try the following after the Update function:

 void OnTriggerEnter2D(Collider2D Coll)
 {
     Coll.transform.parent = transform;
 }

Now go to your platform in you scene view and give the object that has the '$$anonymous$$ovingPlatform' script 2 BoxCollider2D. $$anonymous$$ake on BoxCollider2D as big as the platform (so that you're able to stand on it) and NOT a Trigger and make the other one small in height, but so that it covers the top of the platform, this BoxCollider2D SHOULD be a Trigger. If everything is set up correctly, it should work. Hope this helps!

avatar image meat5000 ♦ · Nov 20, 2014 at 01:32 AM 0
Share

There are lots of these

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Trungdv · Nov 20, 2014 at 01:25 AM

I had the similar problem, but I fixed it. Here is the method: To moving your platform, do not use transform.Translate() in Update(), try using rigidbody2d.movePosition() in FixedUpdate() instead.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do simple moving platform? 1 Answer

How to prevent Character Controller from falling through moving Platforms? 4 Answers

Why is this happening? (gif attached) 2 Answers

Mesh Collider + Character Collider + moving Platforms 0 Answers

Character Controller falls when over Moving Platform 2 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