Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Attach player to moving platform 3d

Discussion in 'Scripting' started by Gabimela, Jan 16, 2020.

  1. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    I am trying to make a player move with the moving rock, the player jumps on it and the rock takes her from a to b but right now the player is just sliding off the rock.

    this is my code for the rock:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerattach : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.  
    9.     public void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.gameObject.tag == "movingrock")
    12.         {
    13.             Player.transform.parent = other.gameObject.transform;
    14.         }
    15.     }
    16.  
    17.     void OnTriggerExit(Collider other)
    18.     {
    19.         Player.transform.parent = null;
    20.     }
    21. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    You can either parent the player temporarily to the object they are on, or drive the player's relative position directly from the rock, which gives you explicit control.

    When the player jumps off or falls off you would have to deparent and/or disable the position driving obviously.
     
  3. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    Olipool likes this.
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    moving platform is a thing that still scares me to implement
     
  5. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    it still doesnt work
     
  6. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Can you give more info like how is the Player in the inspector looking and where does this script go? If it is on the rock then maybe the error is that you check:
    other.gameObject.tag == "movingrock"
    But if the script is on the rock, then the other.gameObject is the Player and not the rock. So change it to:
    if (other.gameObject == Player)...

    Depending on how your Player is setup, you may also want to make the rigidbody kinematic:
    Player.GetComponent<Rigidbody>().isKinematic=true;
     
  7. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    I have played around with it and I have no idea whats wrong with it. I have looked at many many tutorials


    This is what I have:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerattach : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.gameObject.tag == "Player")
    12.         {
    13.             transform.parent = other.transform;
    14.             Player.GetComponent<Rigidbody>().isKinematic = true;
    15.  
    16.         }
    17.     }
    18.     void OnTriggerExit(Collider other)
    19.     {
    20.         if (other.gameObject.tag == "Player")
    21.         {
    22.             transform.parent = null;
    23.         }
    24.     }
    25. }
     
  8. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Can you make a screenshot of your scene with the Player and the rock and the trigger of the rock. Best select the rock and include the inspector in the screenshot.
    The script above has also a mistake I think because you set the parent of the rock to the player and not the other way around :) Try:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    4.         {
    5.             Player.transform.parent = this.transform;
    6.             Player.GetComponent<Rigidbody>().isKinematic = true;
    7.         }
    8.     }
    Does the Player have a Rigidbody?
     
  9. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    This is the player and the rock inspector
     

    Attached Files:

  10. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    This didn't work either :(
     
  11. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Ok, you don't have a trigger on the rock! So OnTriggerEnter will not be called. If you have one of those errory, try to use a debugger or easier use Debug.Log("I am now in OnTriggerEnter"); And if this does not appear in your console than you know this function is never called :)
    So, add a simple BoxCollider to the rock and check "Is Trigger". Edit it so that it is slightly above the rock above the mesh colider.
    If this still does not work it may be because of the Third Person Character scripts. But try adding the trigger first.
     
  12. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    IT WORKS!!!
    however, new error, the player can't move when its on it!
     
  13. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Hm my guess would be that the player controller somehow uses the rigidbody to move the character. Was it an asset or is it the Unity controller? So maybe don't set kinematic to true and if this gives you trouble (most likely it will) then have a look into using a FixedJoint instead of parenting like Serinx mentioned above. I'm off to bed for today, good luck!