- Home /
How to make a platform start moving when the player steps on it
Here's the script I've tried so far
using UnityEngine;
using System.Collections;
public class Moving_Platform_Script_2 : MonoBehaviour {
public Transform position1;
public Transform position2;
public float speed= 15f;
public bool Switch = false;
public bool start=false;
public GameObject player;
public GameObject platform;
void FixedUpdate(){
if(start==true){
// For these 2 if statements it's checking the position of the platform
if(transform.position==position2.position){
Switch=true;
}
if(transform.position==position1.position){
Switch=false;
}
if(Switch){
transform.position=Vector3.MoveTowards(transform.position, position1.position, speed*Time.deltaTime);
}
else{
transform.position=Vector3.MoveTowards(transform.position, position2.position, speed*Time.deltaTime);
}
}
}
void OnCollisionStay(Collision player){
if (Switch) {
player.transform.position=Vector3.MoveTowards(transform.position, position1.position, speed*Time.deltaTime);
}
else {
player.transform.position=Vector3.MoveTowards(transform.position, position2.position, speed*Time.deltaTime);
}
}
void OnCollisionEnter (Collision player){
if (start == false) {
start = true;
}
}
}
Any help would be appreciated
So what's happening now? It doesn't move at all? Based on your script I think the platform should be moving all the time, but what you are doing with the player isn't right.
Ins$$anonymous$$d of moving the platform by setting a new position, it's a lot easier (and better) to use .AddForce ins$$anonymous$$d. This way, the physics-system will calculate it's new position and incorporate the movement of the player (or any object standing on the platform).
What happens is that I the playersteps on the platform and it doesn't move
and I don't use addforce because I intend it to move at the same speed indefinitely
Answer by Mikael-Gyth · May 06, 2014 at 07:39 PM
are you sure transform.position==position1.position is true? That is what I would have checked first, since everyhting else looks ok at first glance.
Other than that I'd make sure that the colliders collide properly.
Also be sure that the player has a rigidbody and collider attached.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144413im_/https://answers.unity.com/themes/thub/images/avi.jpg)