• 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 /
  • Help Room /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RGI · Jan 04, 2013 at 02:11 PM · javascriptcollisionrigidbodyaddforceoncollisionenter

rigidbody.Addforce stacking?

Hi, I have an object moving and I want it to jump whenever it collides with object named "red". This is the script

 var jumpheight : float = 800;
 
 
 function OnCollisionEnter(collision : Collision) {
 
 
 
 if(collision.collider.name=="Red")
    {
  rigidbody.AddForce(Vector3.up * jumpheight);
    }
 
 
 
 
 
 
 
 }

It works the first time it colides with this object but the second time it jumps like 2 times higher than it should. Then it works normally again and later on it jumps 2 times higher again.

EDIT: This is how the scene looks from a side. When the ball touches the red block the script makes it jump. alt text

umm, Strange thing just happend, I tried removing the problematic blocks and copied the ones that work, moved them and they seem to work. When I remove them and create them again, they work o.O What the heck is going on?

Ok, I think I finally know why its happening but I will need help on fixing this thing. I think it is like this: alt text When the sphere moves on the blue line, it jumps normally but when it moves on the red line, so its floating in the air for a second, then it will jump a lot higher for some reason.

problem.png (6.4 kB)
výstřižek.png (20.6 kB)
Comment
Add comment · Show 2
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 MarkFinn · Jan 04, 2013 at 02:23 PM 1
Share

Are you sure it's always on the second occasion?

Could it be from colliding a second time before the jump has moved it clear?

avatar image Karsnen_2 · Jan 04, 2013 at 02:31 PM 0
Share

Thats is what I think too.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Lovrenc · Jan 04, 2013 at 02:27 PM

As MarkFinn said. Your character is coliding while IN JUMP and is adding force. What you should do is start using states (walking, jumping, ...).

When your character is in jumping state you ignore collisions and dont add force.

eg.

 if(collision.collider.name=="Red" && currentState != States.Jumping)
 {
   rigidbody.AddForce(Vector3.up * jumpheight);
 }
Comment
Add comment · 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
1

Answer by Karsnen_2 · Jan 04, 2013 at 02:31 PM

RGI - >

I hope that there is no other script which is influencing your object. I would suggest you to use "tag" instead of "name". If you are not aware of it, you could find it here -> http://docs.unity3d.com/Documentation/Components/class-TagManager.html

Now tag the collider and check for collision like this,

 function OnCollisionEnter(collision : Collision)
  {
 if(collision.collider.tag == "Enemy")
      rigidbody.AddForce(Vector3.up * jumpheight);
 }

Now try to run it. It should work.

If it does not work, then you need to record events and see where the problem is. For example,

 function OnCollisionEnter(collision : Collision)
  {
 if(collision.collider.tag == "Enemy")
 {
      rigidbody.AddForce(Vector3.up * jumpheight);
 Debug.log("Contact with the collider");
 }
 }
 
 
 function OnCollisionExit(collision : Collision)
  {
 if(collision.collider.tag == "Enemy")
      Debug.log("Contact with the collider -    EXITED");
 }

By doing this way, you could know what is happening inside. For example if you object is exits the collider and then enters the colliders once again then force is being applied twice. I can only speculate from here, but if you more help let me know.

cheers!!!

P.S. : I am not good in UnityScript, beware you might get syntax errors.

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 RGI · Jan 05, 2013 at 04:29 PM 0
Share

Did not help, and I dont see any way how this could have solved the problem..

avatar image Owen-Reynolds · Jan 05, 2013 at 06:10 PM 2
Share

The part about adding a print in collisionEnter would definitely help. You'd see right away if the double jumps were caused by double hits. Even better, print "hit "+ collision.transform.name. Sometimes that can pick up hitting stuff you never even thought of.

The part about tag vs. name. Sure, good habit, but nothing to do w/the question. And even then, CompareTag is the official way (I think it runs a hash, so is a little faster than ==. And gives helpful errors if you misspell the tag name.)

avatar image Karsnen_2 · Jan 07, 2013 at 02:16 PM 1
Share

The tag vs name thing is just a good habit. I am pretty sure RGI that it would help to find where the problem is. I did mention that, it would help to find what is happening inside and not giving you soln. We can't provide solution here, we can only help you to figure out the solution.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Apply Force when a Rigidbody ExitCollider 2 Answers

How to check fo rigidbody collision? 0 Answers

Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers

Collider seems bigger than it is 0 Answers

Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer


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