- Home /
Collision problem
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();
}
Answer by Catlard · Mar 12, 2013 at 05:04 PM
Are you parenting your player to the moving platform when he lands on it? That way, it will automatically stand on the platform correctly.
I don't know what kind of player you're using, but the player code that comes with Unity does not use a rigidbody, and one of your colliding objects must be a rigidbody for a collision data object to be generated. If that's what you're using (the out-of-the-box player), then you should just attach a rigidbody shape to the player so that it will also hit your moving platform at the same time as the player. Is that what's going on? Let me know.
Also, where are you changing the value of the variable "up"?
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
Yeah, 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.
I 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?
Your answer
Follow this Question
Related Questions
How to move multiple dynamic gameobjects with one moving platform which is controlled by the player 0 Answers
Moving a 3D player without player controller 1 Answer
2d Platformer Jumping Not Working 1 Answer
How to prevent Character Controller from falling through moving Platforms? 4 Answers
One-way platform's side causes collision with Platform Effector 1 Answer