- Home /
Merge 2d colliders, slow down character after collision
Hey there,
I'm still working on a 2D Infinite Runner, and currently I encountered the following issue:
I scripted and created the 2D Character as in the Tutorial of the Learning section, which means I gave him 2 different colliders: 1 Box collider with a slippery material and 1 circle collider for the ground detection. For the game I want to limit the amount of collisions the character may have with obstacles to 3 which I already did, but the issue with the collisions is, that it will always count both colliders in. So if the character enters the trigger of an obstacle, the count will increase by 2 because of the 2 colliders. I've already thought about the Physics2D.IgnoreCollision method to limit the count to the boxcollider only, so that only 1 collision will be counted. But then his feet may touch an obstacle and nothing happens which bothers me...is there any other way to make it count both collisions as one?
The second issue is, that I'd like to slow down the character after a collision, but I don't know how...I'd tried to change the rigidbody2D.velocity.x, but nothing happens...
If somebody could help me, that would be great! Thanks in advance!
Answer by stevecus · Jan 20, 2015 at 03:51 PM
You could trying using a bool to limit the trigger time, almost like sonic or super mario when you get hit you have a window when you cant be consecutively hit. something like....
As for the speed you character should have a speed variable? Just reference the script so if colldier hit > player.speed = 2;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Boulder" && characterJustBeenHit == false;)
{
startCoroutine(CollisionCooldown())
}
}
IEnumerator CollisionCooldown()
{
health -= 1;
characterJustBeenHit = true;
print("Character Cant be Hit");
yield return new WaitforSeconds(3)
print("Character Can Now be Hit!!!");
characterJustBeenHit = false;
}
This is in c# hope it helps.
Steve.
Thank you VERY much! This works absolutely fine on the collision issue!! :D
just one little mistake which I had to fix in the script: StartCoroutine (->uppercase "S")
about the speed decrease:
here is the code which controls the character movement
if ((grounded || airControl) && !dead) {
float move = 1.0f; //Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", $$anonymous$$athf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
I should mention that in the end game it is planned that the player is chased by an enemy, so for each hit the character must slow down a bit. If the player hits e.g. 2 obstacles in a row, he shall be caught and thus the player looses. Unfortunately I have no idea how to change the speed in a way that it'll decrease by hitting an obstacle and increase again afterwards to the maximum speed unlike the player hits another obstacle during this time period.
I tried to simply add a new rigidbody.velocity.x for each hit, but it didn't work out as intended...
your velocity is defined in the x axis by move * maxSpeed. $$anonymous$$ove is 1.0f so the speed of your character is defined by maxSpeed.
Try adding a line of script after being hit by the collider in your coroutine like
maxSpeed = (lower value)
or alternatively just have some lines in Update() like this...
If(health == 3)
{
maxSpeed = 15;
}
if(health == 2)
{
maxSpeed = 10;
}
if(health == 1)
{
maxSpeed = 5;
}
If you want to have the speed lower as they are hit then slowly increase back up to maxSpeed you need two variables. Replace your maxSpeed with just speed, and have a maxSpeed value.
speed = 3.0f
speed += Time.DeltaTime;
if(speed >= maxSpeed)
{
speed = maxSpeed;
}
Could you please up vote my answer if it has solved the issues, Thanks.
Your answer
Follow this Question
Related Questions
add camera collision without CharacterController 1 Answer
Voxel engine physics 3rd person character controller 1 Answer
Character mouvement and collisions in arbitrary angled surface 1 Answer
Problems with character controller 1 Answer
Detecting collisions with OnControllerColliderHit when not moving 0 Answers