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 /
This question was closed Apr 26, 2018 at 02:16 PM by fghfghfg for the following reason:

I solved it myself, solution has been left for others

avatar image
0
Question by fghfghfg · Apr 26, 2018 at 01:09 PM · physicsrigidbodycharacter movementstuckmoving platform

Rigidbody character cannot move once parented to platform

I searched my buns off looking for a solution but couldn't find any sorry for asking :(

My problem is that I have a rigidbody character and when I land on a moving platform I make the platform a parent of the character and the character moves with the moving platform which is the desired effect. However the movement controls no longer work once I'm on the platform, my character is effectively glued to the platform indefinitely. I have trouble shooted the bajeebas out of this and have had no luck, many thanks in advance for any light you're able to shed on this.

I have attached both my platform and character scripts

Here is the characterController script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class PlayerController : MonoBehaviour {
 
     public AudioSource coinAudioSource;
     public AudioSource goalAudioSource;
     public AudioSource jumpAudioSource;
 
     public float walkSpeed = 8f;
     public float jumpSpeed = 7f;
     public float turnSpeed = 2f;
 
     public bool isController2D = true;
 
     public bool pressedJump = false;
     public bool isJumping = false;
     public bool isGrounded = true;
     public bool isHeading = false;
 
     public HudManager hud;
 
     Rigidbody rb;
     Collider coll;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody> ();
         coll = GetComponent<Collider> ();
         hud.Refresh ();
     }
     
     // Update is called once per frame
     void Update () {
         //TurnHandler ();
         if (isController2D) {
             SideWalkHandler();
         } else {
             WalkHandler ();
         }
         JumpHandler ();
     }
 
     bool checkGrounded(){
         //get player collision dimensions
         float sizeX = coll.bounds.size.x;
         float sizeY = coll.bounds.size.y;
         float sizeZ = coll.bounds.size.z;
 
         //find the bottom points of the player
         Vector3 corner1 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
         Vector3 corner2 = transform.position + new Vector3 (-sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
         Vector3 corner3 = transform.position + new Vector3 (sizeX / 2, -sizeY / 2 + 0.01f, -sizeZ / 2);
         Vector3 corner4 = transform.position + new Vector3 (-sizeX / 2, -sizeY / 2 + 0.01f, -sizeZ / 2);
 
         //cast rays from bottom four corners
         bool grounded1 = Physics.Raycast(corner1,new Vector3(0,-1,0),0.02f);
         bool grounded2 = Physics.Raycast(corner2,new Vector3(0,-1,0),0.02f);
         bool grounded3 = Physics.Raycast(corner3,new Vector3(0,-1,0),0.02f);
         bool grounded4 = Physics.Raycast(corner4,new Vector3(0,-1,0),0.02f);
 
         //Debug.Log ("Grounded: " + grounded1 + grounded2 + grounded3 + grounded4);
 
         return(grounded1||grounded2||grounded3||grounded4);
     }
 
     bool CheckHeadHit(){
         bool headed1 = false;
         float sizeX = coll.bounds.size.x;
         float sizeY = coll.bounds.size.y;
         Vector3 centerOfTop = transform.position + new Vector3 (0f, sizeY / 2 + 0.01f, 0f);
         Vector3 frontOfTop = transform.position + new Vector3 (sizeX / 2, 0f, 0f);
         Vector3 rearOfTop = transform.position + new Vector3 (-sizeX / 2, 0f, 0f);
         RaycastHit hit;
         if (Physics.Raycast (centerOfTop, new Vector3 (0, 1, 0), out hit, 0.02f)) {
             if (hit.collider.tag == "brick") {
                 hit.collider.SendMessage ("Hit");
                 headed1 = true;
             }
         }
         return(headed1);
     }
 
     void JumpHandler(){
         //get jump axis
         float jAxis = Input.GetAxis ("ButtonA");
         //Debug.Log ("jAxis: "+jAxis);
         isGrounded = checkGrounded ();
 
         //if jAxis exceeds 0f then make the player jump
         if (jAxis > 0f) {
             if(isGrounded && !pressedJump){
                 pressedJump = true;
                 //play jump sound
                 jumpAudioSource.Play();
                 //Jumping Vector
                 Vector3 jumpVector = new Vector3 (0f, jumpSpeed, 0f);
                 //Add jumping velocity
                 rb.velocity = rb.velocity + jumpVector;
             }
         } else {
             pressedJump = false;
             if (isGrounded) {
                 isJumping = false;
             } else {
                 isJumping = true;
                 isHeading = CheckHeadHit ();
             }
         }
     }
 
     void WalkHandler(){
         //set x and z velocities to 0
         rb.velocity = new Vector3 (0f, rb.velocity.y, 0f);
         //get distance
         float distance = walkSpeed * Time.deltaTime;
         Vector3 movement = new Vector3 (0f,0f,0f);
         //get inputs
         float hAxis = Input.GetAxis ("Horizontal");
         //get vertical axis input
         float vAxis = Input.GetAxis ("Vertical");
         //get movement vector 3D
         movement = new Vector3 (hAxis * distance, 0f, vAxis * distance);
         //get current position
         Vector3 currPosition = transform.position;
         //get new position to move to
         Vector3 newPosition = currPosition + movement;
         //move to new position
         rb.MovePosition (newPosition);
     }
 
     void SideWalkHandler(){
         //set x and z velocities to 0
         rb.velocity = new Vector3 (0f, rb.velocity.y, 0f);
         //get distance
         float distance = walkSpeed * Time.deltaTime;
         Vector3 movement = new Vector3 (0f,0f,0f);
         //get inputs
         float hAxis = Input.GetAxis ("Horizontal");
         //get vertical axis input
         float vAxis = Input.GetAxis ("Vertical");
         //get movement vector 2D
         movement = new Vector3 (hAxis * distance, 0f, 0f);
         //get current position
         Vector3 currPosition = transform.position;
         //get new position to move to
         Vector3 newPosition = currPosition + movement;
         //move to new position
         rb.MovePosition (newPosition);
     }
 
     void OnTriggerEnter(Collider collider){
         if (collider.tag == "coin") {
             print ("Grabbing coin...");
             coinAudioSource.Play ();
             GameManager.gminstance.IncreaseScore (1);
             print ("Score: " + GameManager.gminstance.score);
             hud.Refresh ();
             Destroy (collider.gameObject);
         }
         if (collider.tag == "goal") {
             print ("Reached goal!");
             goalAudioSource.Play ();
             Destroy (collider.gameObject);
             if (GameManager.gminstance.currentLevel == GameManager.gminstance.highestLevel) {
                 SceneManager.LoadScene ("you_finished");
             } else {
                 GameManager.gminstance.GoToNextLevel ();
             }
         }
         if (collider.tag == "enemy") {
             print ("Enemy hit, Game Over!");
             SceneManager.LoadScene ("game_over");
         }
     }
 }
 

Here is my platformController script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlatformController : MonoBehaviour {
 
     Vector3 startPosition = new Vector3(0,0,0);
 
     public float moveDistance = 1f;
     public float moveSpeed = 1f;
     public float moveDirection = 1f;
 
     // Use this for initialization
     void Start () {
         startPosition = transform.position;
     }
 
     // Update is called once per frame
     void Update () {
         MoveX ();
     }
 
     void MoveX(){
         //how much are we moving?
         float movementX = moveDirection * moveSpeed * Time.deltaTime;
         //check the new position
         float newX = transform.position.x + movementX;
         //f the new position is too far change direction
         if (Mathf.Abs (newX - startPosition.x) > moveDistance) {
             moveDirection *= -1f;
         } else {
             //if we can move, move
             transform.Translate (new Vector3(movementX,0f,0f));
         }
     }
 
     void OnTriggerEnter(Collider coll){
         if(coll.tag == "player"){
             coll.transform.parent = gameObject.transform;
         }
     }
 
     void OnTriggerExit(Collider coll){
         coll.transform.parent = null;
     }
 }
 

 
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

  • Sort: 
avatar image
0

Answer by fghfghfg · Apr 26, 2018 at 02:15 PM

Sorry guys I solved it, here is the solution for anyone that has the same issue.. What I did was change my movement method from velocity based to translate based.

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

210 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

GetAxis being missed in FixedUpdate work around? 1 Answer

Smooth motion rigidbody 1 Answer

Unity Rigidbody fps controller jumps higher each time. 0 Answers

Objects not acting according to physics 1 Answer

Moving RigidBody Smoothly 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