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

Player falls through a moving platform

Discussion in 'Scripting' started by BisquTz, Mar 4, 2018.

  1. BisquTz

    BisquTz

    Joined:
    Jul 2, 2016
    Posts:
    4
    I have a moving platform set up that makes my Player a child of it when triggered by a collision, however when my Platform is set as a trigger my Player falls through it, here is the code to my moving platform, can anyone help, thanks.

    Code (CSharp):
    1.  
    2. public class PlatformHorizontal : MonoBehaviour {
    3.     public Transform[] target;
    4.     public float speed;
    5.     private Transform currentPlatform;
    6.  
    7.     private int current;
    8.  
    9.     void OnTriggerEntry(Collider other){
    10.         if (other.transform.tag == "MovingPlatform");
    11.             currentPlatform = other.transform;
    12.             transform.parent = other.transform;
    13.     }
    14.     void OnTriggerExit(Collider other){
    15.         if (other.transform == currentPlatform) {
    16.             currentPlatform = null;
    17.             transform.parent = null;
    18.         }
    19.     }
    20.     void Update () {
    21.         if (transform.position != target [current].position) {
    22.             Vector3 pos = Vector3.MoveTowards (transform.position, target [current].position, speed * Time.deltaTime);
    23.             GetComponent<Rigidbody> ().MovePosition (pos);
    24.         } else current = (current + 1) % target.Length;
    25.     }
    26. }
    27.  
     
    Last edited: Mar 4, 2018
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You need to use code tags:
    Are you getting any errors, because you use OnTriggerEntry instead of OnTriggerEnter
     
  3. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
  4. BisquTz

    BisquTz

    Joined:
    Jul 2, 2016
    Posts:
    4
    No, not getting any errors
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Does your code actually use OnTriggerEntry? Because it won't work that way.
     
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    As above, you probably should change OnTriggerEntry to OnTriggerEnter.