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 thebattleaxe666 · Jun 07, 2018 at 01:33 PM · physicsraycastaddforcerayhit

Trouble with Ray & Raycasting and AddForce()

In my code, when you click on the screen, the sphere starts in the direction of the finger tap. It uses raycast. The sphere is launched only if the raycast hit contains information about the rigidbody-body. However, if I shoot raycast somewhere in the sky, then this code, in fact, should be ignored. However, no, the sphere will spawn somewhere above the stage and fall, although this should not have happened. What is the problem?


 private IEnumerator SpawnSphere(Vector2 position)
     {
         yield return new WaitForSeconds(0.1f);
 
         if (Input.touchCount == 0)
         {
             GameObject shell = Instantiate(shellPrefab) as GameObject;
             Ray ray = Camera.main.ScreenPointToRay(new Vector3(position.x,position.y));
             RaycastHit hit;
 
             if(Physics.Raycast(ray, out hit))
             {
 
                 if (hit.rigidbody != null)
                 {
                     shell.GetComponent<Transform>().position = transform.position;
                     shell.GetComponent<Transform>().LookAt(hit.point - transform.position);
    shell.GetComponent<Rigidbody>().AddForce((hit.point - transform.position), ForceMode.Impulse);
 
                 }
             }
         }
         yield return null;
     }
 }



Besides this, there is one more problem. See this line?

 shell.GetComponent<Rigidbody>().AddForce((hit.point - transform.position), ForceMode.Force);

In this sphere code, a force is added that moves this sphere into a certain vector. However, the force varies depending on the distance to this vector! What should this vector (or other operation) be multiplied for, so that the sphere can be launched with the same force regardless of distance?


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
Best Answer

Answer by Captain_Pineapple · Jun 07, 2018 at 01:48 PM

Hey there,

the solution to your first problem is, that you spawn the shell either way. You don't check for a raycast hit before spawning it, and you don't remove it when you don't hit anything. In case there is no hit the shell will spawn at the position given in the prefab and will not be further handled.

For your force problem you have 2 options: either use Vector3.Clamp() to clamp the direction vector to a given length.

Or:

Use something like: (hit.point -transform.position)/Vector3.Magnitude(hit.point-transform.position) This will normalize the vectors length to 1.

OR:

Use (hit.point -transform.position).normalized

Hope this helps.

Comment
Add comment · Show 2 · 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 thebattleaxe666 · Jun 07, 2018 at 02:04 PM 0
Share

Well, the problem with force, I decided, something like this helped, the sphere flies with the same speed, thanks

  shell.GetComponent<Rigidbody>().AddForce((hit.point - transform.position) / Vector3.$$anonymous$$agnitude(hit.point - transform.position) * 1000, Force$$anonymous$$ode.Impulse);

But with Ray I did not understand a bit - should I delete the RaycastHit object? Or each time to do a new one like "..new RaycastHit .." or how? how it should look like in the code?

avatar image thebattleaxe666 · Jun 07, 2018 at 02:09 PM 0
Share

Oh damn, I understand it, it will spawn anyway, thanks for the answer

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

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

Need Help with RayCast. No Vector? 1 Answer

how to select an object with raycasting 1 Answer

Click&Drag Misterious Disappearing! 1 Answer

Raycast retuning info of hit object 1 Answer

Problems with Raycast/AddForce on a Ball in 3D 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