I’m making a game to just try and learn the ropes of unity as I’m too good with it. I’m making a simple platformer with an up and down moving platform. However when I set the platform to start moving when the player collides with it, nothing happens. I know there’s nothing wrong with my movement script as it works perfectly when I have it set to be on at the start.
If someone could look at my collision code and tell me why nothing’s happening it would be appreciated.
var speed : float; //speed at which the platform moves
var startOn : boolean; //If the platform should move at the start
var pause : float; //How long it pauses for
var onTime : float = 0.0; //amount of time that the object will move before pausing
var up : boolean; //direction the platform moves in
var target : Transform; //What must collide with the platform for it to start moving
function OnCollisionEnter (collision : Collision)
{
if (collision.gameObject == target)
{
if (startOn == false)
{
startOn = true;
}
}
}
function Movement () {
var controller : CharacterController = GetComponent(CharacterController);
if (up == true)
{
controller.Move(Vector3.up * speed * Time.deltaTime);
}
if (up == false)
{
controller.Move(Vector3.down * speed * Time.deltaTime);
}
if (Time.time > onTime) {
startOn = false;
yield WaitForSeconds (pause);
onTime = Time.time + onTime;
startOn = true;
}
}
function Update () {
if (startOn == true)
Movement();
}
I was still working on the platform movement but i'm just about done with that part, the up variable is being changed by the user in the component menu and in the code i have now. That said, I do have a rigidbody on the player object but when I go to stand on it it doesn't move. Also I heard parenting objects to each other for moving platforms isn't the best choice, I want the player to be able to move around while he's on top of it. Thanks for the reply
– DoctorMoneyYeah, parenting the object to the platform will allow you to move the player on that platform...it works for me. Anyway. As far as rigidbody, make sure that the rigidbody object is actually the object that you're detecting a hit for. And try using other objects with rigidbodies. Is it just the player htis is happening with? Let me know.
– CatlardI changed the "target" variable from a Transform to a Rigidbody and it didn't make a difference. It is testing to see if player collides with the platform unless my code is wrong for the collision detection. I don't see how there is anything wrong with my script though. How would I go about doing the parenting?
– DoctorMoney