- Home /
Moving platform
Hi there i am trying to make a moving platform that will start to move when the player is on the trigger, i got a working trigger but i can make the platform move. i started with this code but i have absolutely no idea how continue it, please help!!!!
var platform : Transform;
function OnTriggerEnter(col: Collider)
{
if(col.tag == "Player")
{
col.transform.parent = transform.parent;
}
}
function OnTriggerExit(col: Collider)
{
if(col.tag == "Player")
{
col.transform.parent = null;
}
}
Answer by tylo · Oct 28, 2013 at 07:43 PM
Try parenting the platform to the player, instead of the player to the platform. Then, the platform will move with the player when you use the player controls.
If this isn't the the behavior you want, then leave a comment on this answer and provide more info on what exactly you want the platform to do.
Basicly I need that the platform moves when the player is on it and stays on it while moving from A to B; in case the player jumps off the platform this just stops.
Answer by FirePlantGames · Oct 29, 2013 at 06:01 AM
try this:
var platform : Transform;
function OnTriggerEnter(col: Collider)
{
if(col.tag == "Player")
{
col.transform.parent = null;
}
}
function OnTriggerExit(col: Collider)
{
if(col.tag == "Player")
{
col.transform.parent = null;
}
}
i don't really understand your problem though
Ok maybe i did not be clear, i need a platform that moves only if the player is standing on it. btw your script works but the platform does not move.
Answer by fafase · Oct 29, 2013 at 12:17 PM
Use a boolean to define when the guy is on:
var isOn : boolean = false;
var basPos : Transform;
var target : Transform;
var step : float = 0.5f;
function OnTriggerEnter(col:Collider){
if(col.tag == "Player")
isOn = true;
}
function OnTriggerExit(col:Collider){
if(col.tag == "Player")
isOn = false;
}
function Update(){
if(isOn)
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
else if(tranform.position != basePos.position)
transform.position = Vector3.MoveTowards(transform.position, basePos.position, step);
}
This is just a start . You can improve it later but you can strt off with this already.
Ok i worked on it and now i got this:
using UnityEngine; using System.Collections;
public class $$anonymous$$ovingPlatform_CSHARP : $$anonymous$$onoBehaviour {
public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch= false;
void FixedUpdate()
{
if(Switch)
{
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, DestinationSpot.position, Speed);
}
else
{
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, OriginSpot.position, Speed);
}
}
void OnTriggerEnter (Collider other)
{
Debug.Log ("PlayerIn");
if(transform.position == OriginSpot.position)
{
Switch = true;
Debug.Log ("PlayerStarts");
}
if(transform.position == DestinationSpot.position)
{
Switch = false;
}
}
}
It's working but if i don't make the player child of the platform, he is catapulted random.
Yes but you had the parenting already.I thought you were after the moving of the platform.
I really thank all of you for the help, the platform now moves when the player steps on it, and i don't even need to parent them!!!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to import the object from server to unity 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Material doesn't have a color property '_Color' 4 Answers
Setting Scroll View Width GUILayout 1 Answer