- Home /
Platforming Collision Help
I'm having some issues with my Mario clone game. First I don't like the way my ground detecting works right now because I feel like it slows the character down when he lands, maybe its me, but here is an example of a collision on the floor.
if (other.tag == "Floor")
{
transform.position.y = 3.028204;
grounded = true;
if (faceRight)
animation.Play("IdleRight");
else
animation.Play("IdleLeft");
}
Notice I transform the position to a specific y value a feel like for a split frame while its adjusting the y value the player is frozen, maybe I'm just imagining things. If I exclude this part of the script they stop, but many times it is later then I would like and Mario ends up through part of the floor or object he landed on.
Another issue I am running into is with floating blocks I have 3 collision box's for each side of them and when I jump really close to the the side of one and hold into the box I get a lot of random results, either he hits it and bounces down (which is fine), gets stuck on the corner for a second and either pushed out or on top, or he teleports to the top (this is due to a similar collision as the floor that sets the y value.
I tried adjusting the collision box sizes, but nothing seems to work, maybe I should move the side boxes out more?
The second issue I have is with my enemies. Right now I have a hitbox on their head to my character to land on. This works fine, what I noticed was when I walked into them and died they still played their death animation. Turns out it was detecting if the hitbox from my character touched ANY of the collision boxs the enemy had. I only want it to do the head, since the script was attached to the actual enemy entity it does this, if I apply it to the head it doesn't:
if (other.tag == "MarioFeet")
{
gameObject.animation.Play("GoombaDeath");
GoombaAIScript.move = 0;
MarioMove.jumpSpeed = 0.3;
}
Since the script is on the actual object it plays the animation which triggers the destroy function in the events. The questions is how do I get the head collision box to detect the game object its attached to play the animation? Also is it possible to do this in a script not attached to the head? I hate having a script on so many different pieces of a character/enemy.
The third thing is the camera I made a script to follow Mario when he hits a collision box and is hold right the camera follows otherwise it stops. Problem is I am finding a lot of times where I clip through the collision box. I initially found if I held left first then right I would walk through it, I fixed this by making sure as long as left was being held to do it. Right now I can get through the collision if I fake it out by pressing left quickly then right. Any solutions?
Last questions not specific to collision, but is it normal to have a TON of tags? I have to have at least 50+ right now.
Anyway thank you for the help in advanced.
Answer by Montraydavis · Nov 04, 2012 at 07:18 PM
You're going about this the wrong way .
For starters, it's much simpler than that to get collision working. That y = 3 . . . Not a single clue why you would want to do that . . . Not unless you have very specific reasons. If you really really need the side boxes, set them to "trigger", or, Physics.IgnoreCollision ( )
Example:
var bulletPrefab : Transform;
function Start () {
var bullet = Instantiate(bulletPrefab) as Transform;
Physics.IgnoreCollision(bullet.collider, collider);
}
Add a Box Collider and Rigidbody to your Mario player, and your collision issue should be fixed. It doesn't take much for proper collision . If you already have colliders, you need to make sure that they are not set as "trigger", have rigidbodies, and are positioned correctly.
Add this script to mario
function OnCollisionEnter ( collision : Collision )
{
if ( collision.collider.tag == "Ground" )
{
grounded = true ;
}
}
function OnCollisionExit ( collision : Collision )
{
if ( collision.collider.tag == "Ground" )
{
grounded = false ;
}
}
Secondly, maybe you have a specific purpose for the side boxes, but, I don't really find them to be necessary from what I have read. . . And also, I don't quite understand what's going wrong on part 2 of your issue, but I will try my best to explain where you are going wrong. ( PS: Please bare with me if I am not understanding correctly )
I don't see where you destroy the object after the animation is done. Perhaps add this Kill function ?
function KillEnemy ( )
{
animation.Play ( "GoombaDeath" ) ;
yield WaitForSeconds ( animation["GoombaDeath"].clip.length + 0.1 ) ;
GoombaAIScript.move = 0;
MarioMove.jumpSpeed = 0.3;
GameObject.Destroy ( gameObject ) ;
}
For the third issue, I just can't comment on without any sort of explanation as to what your code does, or, even better, a script that I can reference from.
I hope this answer solves your issues, and if so, please mark as answered/vote . Thanks for using Unity3D, and, best of luck to you sir/ma'am .
Using your code unfortunately did not solve the problem, $$anonymous$$ario has 3 collision boxes, one on the main object, one that's a hurtbox that triggers death, and another is feet that I was experimenting with for the goomba death.
I have removed the trigger check mark on all the boxes, made it so theirs only one and tried all of that with the floor being a ridged body as well, but I still have him fall through the floor. Not sure what I am doing wrong
The "side boxes" I referring to is actually just the ? (Question) blocks I need to make sure the player doesn't go through the side of the box, I have separate collision boxes because I can't figure out if there is a way to get it to detect which side of a full cube collision box is hit (aka when he punches the block) Collisions have been giving me a headache since day one I've been using triggers this whole time if you know how to make it easier please tell me.
As for the goomba death. The animation triggers and event at the end that sets off the Death() function which destroys the object. The issue I am having right now is that no matter which collision box is hit on the enemy it trigger the death animation, even if $$anonymous$$ario is killed to. I wanted the script to be on the main object ins$$anonymous$$d of each individual collision box, but even if it was on the collision box It would need to detect which Goomba (out of the 10 or so that are in the level) is the one to destroy when it gets hit, is their a way for the collision box to detect the object its attached to? I see functions for children components, but non for parent.
The way I have the camera is setup as such. If $$anonymous$$ario hits the trigger and is holding right the camera follows if he hits left it stops and he has to hit the trigger again to continue moving the camera. The camera has this trigger and a collision box with a -speed trigger on it to prevent backtracking. The problem with either of these although the back collision box seems to work fine now the front one unfortunately I find myself running through the trigger and thus breaking the camera. $$anonymous$$y Code:
On $$anonymous$$ario
if (other.tag == "Start Camera Trigger" && Input.Get$$anonymous$$ey("right")) { cameraFollow.cameraFollowZ = true; }
On the camera
if (Input.Get$$anonymous$$ey("left") && !Input.Get$$anonymous$$ey("right")) cameraFollowZ = false;
Answer by Bakuryu42 · Nov 13, 2012 at 10:28 PM
I've been messing with this for a while now I still can't get it I don't understand. Is it because I'm using a ridged body an is Kinetic for the character? If so what should I use?
Your answer
Follow this Question
Related Questions
OnCollisionEnter not called when colliding with clone 0 Answers
Character Controller falls through moving/rotating platforms (Not A Duplicate Post) 1 Answer
One Way Collider 5 Answers
My sprite keeps falling through my ground 3 Answers
[2D Platformer] How do i make a platform that is only active when the player is standing on it? 1 Answer