Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Xysch · Feb 26, 2018 at 06:49 PM · bounceraycasthit2daddrelativeforce

Inconsistent AddForce with raycasting

Hi, I am currently trying to find a solution to my problem but just can't seem to get it right. What I want to happen is when the raycast collides with the ground, the player is launched toward a direction based on the angle of the collider/ground.


My issue is that the AddFroce does not seem consistent in always bringing the player to a certain height, and will sometimes shoot the player high into the air and other times not at all. Here is my code to do this:

 private void Update()
 {
     RaycastHit2D hit = Physics2D.Raycast(raycastStartPoint.position, -Vector2.right, 0.001f);
     if (hit.collider != null)
     {
         if (hit.collider.name == "Ground" || hit.collider.name == "LeftWall" || hit.collider.name == "RightWall")
         {
             if (shouldAddForce == true)
             {
                 direction = -hit.point;                         // Find the point of collision
                 shouldAddForce = false;                         // Does not allow for antoher collision
                 StartCoroutine(AllowAnotherShield());           // Calls to end no more collisions
                 objectRigidbody.velocity = Vector2.zero;        // Stops the player's velocity so add 
                                                                 // force does not have to counter the 
                                                                 // current velocity
             }
         }
     }
     if(shouldAddForce == false)             // If we should be adding force
     {
         AddForce();
     }
 }

 private void AddForce()
 {
     objectRigidbody.AddRelativeForce(direction * force);        /// Adds force at direction
 }

 private IEnumerator AllowAnotherShield()
 {
     yield return new WaitForSeconds(seconds);        // Waits for seconds
     shouldAddForce = true;                           // We can find another collision
 }

}

Here is a youtube video on what my game looks like and you can see sometimes the player flies way high and other times it does not: https://www.youtube.com/watch?v=cjQuYr8Af6U&feature=youtu.be

Comment
Add comment · Show 1
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 TreyH · Feb 26, 2018 at 08:14 PM 0
Share

Physics operations should happen within FixedUpdate(). Try changing your update function to FixedUpdate (which shouldn't be an issue as you don't have any inputs being captured here) and see the the problem persists.

Also, is your shouldAddForce check backwards at the bottom?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by seandolan · Feb 27, 2018 at 04:51 AM

I could watch that video all day. It was so addictive. You could try this instead:

 objectRigidbody.AddForce(objectRigidbody.transform.TransformDirection(direction) * force,ForceMode.Impulse);

If this doesn't work, try swapping around the ForceMode to one of the other options.

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

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

76 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i shoot 2D ball/bounce of wall using Raycast? 1 Answer

Object moves too fast after bouncing off of a moving collider,objects move too fast after bouncing off a moving collider 2 Answers

LinecastNonAlloc, documented syntax gives error... 1 Answer

What's wrong with my raycasthit2D? 2 Answers

Unity Physics2D.Raycast always returns “the invoker” as hit 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