- Home /
Question by
DimitriTheFox · Oct 07, 2016 at 12:47 PM ·
c#triggerschild object
Moving platforms in unity 3D
I'm trying to make a moving platform that also takes the player with it when the players stands on it. I have done the exact same thing as show in this tutorial. https://www.youtube.com/watch?v=AfwmRYQRsbg
Scripts form the tutorial:
using UnityEngine;
using System.Collections;
public class MovingPlatforms : MonoBehaviour {
public Transform movingPlatform;
public Transform position1;
public Transform position2;
public Vector3 newPosition;
public string currentState;
public float smooth;
public float resetTime;
// Use this for initialization
void Start () {
ChangeTarget();
}
// Update is called once per frame
void FixedUpdate () {
movingPlatform.position = Vector3.Lerp (movingPlatform.position, newPosition, smooth * Time.deltaTime);
}
void ChangeTarget () {
if(currentState == "Moving To Position 1")
{
currentState = "Moving To Position 2";
newPosition = position2.position;
}
else if (currentState == "Moving To Position 2")
{
currentState = "Moving To Position 1";
newPosition = position1.position;
}
else if (currentState == "")
{
currentState = "Moving To Position 2";
newPosition = position2.position;
}
Invoke ("ChangeTarget", resetTime);
}
}
using UnityEngine;
using System.Collections;
public class HoldPlayer : MonoBehaviour {
void onTriggerStay(Collider col) {
col.transform.parent = gameObject.transform;
}
void onTriggerExit(Collider col)
{
col.transform.parent = null;
}
}
The moving part works, but the player (or any other object that enters the trigger) doesn't become a child of the platform. Does anyone know what to make this work. It would really help me out here. Thanks in advance. (Sorry for any mistakes, I'm Dutch so my English isn't that great)
Comment
Answer by tanoshimi · Oct 07, 2016 at 01:04 PM
Case-sensitivity matters. It's OnTriggerStay
and OnTriggerExit
, not onTriggerStay
and onTriggerExit
.