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 Heav3n · Nov 11, 2012 at 04:45 AM · collisionrigidbodyjumpaddforce

jump on collision weird behaviour

Hello I have this script below. I want only to move the character left or right and when he collides to give him a jump effect. It seems to work fine. But when the character collides and its on the air and collides again with a new object of same type the jump effect is doubled or tripled and its not like the normal one. If it collides with the same object again the jump effect is back to normal. Anyone knows whats wrong?

 var speed : float = 5;
 var height : int = 150;
 var x : float;
 
 public var distanceTravelled : float;
 
 function OnCollisionEnter( collision : Collision ) {
         rigidbody.AddForce (Vector3.up * height);
 }
 
 function Start () {
     
 }
 
 function Update () {
      x = Input.GetAxis("Horizontal") * speed * Time.deltaTime; 
      transform.Translate(x, 0, 0);
      distanceTravelled = transform.localPosition.y;
      Debug.Log("Distance:" + distanceTravelled);
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Heav3n · Nov 11, 2012 at 02:56 PM

I did it with

 rigidbody.velocity

instead of rigidbody.AddForce

and seems to work like i want :)

Thanks for the help

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 KiraSensei · Nov 11, 2012 at 12:33 PM

On each collision you add a force. Instead of adding a new force, you need to "forget" the old force you applied first. You can add the opposite force to the old one before adding the new one for example. I don't see any method to do it easier. If someone knows one ...

Comment
Add comment · Show 6 · 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 Heav3n · Nov 11, 2012 at 12:46 PM 0
Share

Thanks for your answer. Could you explain with code where do I need to "forget" about it?

avatar image KiraSensei · Nov 11, 2012 at 12:51 PM 0
Share

You can "store" the previous force you applied in a variable, and when another force must be applied, just before you add the opposite of the previous force. It is only a trick to easily bypass the problem. There should be a smoother solution but I don't know it :)

avatar image Heav3n · Nov 11, 2012 at 01:08 PM 0
Share

it doesn't move the second time.

avatar image KiraSensei · Nov 11, 2012 at 01:17 PM 0
Share

Can you show me your code then ?

avatar image Heav3n · Nov 11, 2012 at 01:29 PM 0
Share

var firstTime : boolean = true;

function OnCollisionEnter( collision : Collision ) { if (firstTime) { rigidbody.AddForce (Vector3.up height); } else { rigidbody.AddForce (Vector3.down height); rigidbody.AddForce (Vector3.up * height); firstTime = false; } }

Show more comments

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

10 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

Related Questions

Physics AddForce reduced when 3 objects are colliding 1 Answer

Keep Horizontal Momentum after Jump 2 Answers

Why object goes sometimes through walls by adding force ? 2 Answers

moving with rigidbody without acceleration 0 Answers

RigidBody collision is unaccurate 2 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