Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CodingNoob · Jan 20, 2015 at 11:49 AM · collisioncharactercharactercontrollerslowdown

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CodingNoob · Jan 20, 2015 at 06:08 PM 0
Share

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...

avatar image stevecus · Jan 20, 2015 at 06:53 PM 1
Share

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;
 }

avatar image stevecus · Jan 23, 2015 at 03:52 PM 0
Share

Could you please up vote my answer if it has solved the issues, Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges