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 TheDarkenedPoet · Mar 17, 2015 at 12:51 PM · collisionraycastpositionspherecasttrigonometry

Using Trigonometry for Collision Detection

Okay, so for the past few days I have been trying to figure out how to use a sphereCast to restrict the movement of my player. The sphereCast essentially represents the players weapon and during certain states the length of that sphereCast needs to dictate the location the player can move to.

I am not the strongest at maths but I think I'm sort of on the right track, the main thing I'm struggling with at the moment is how to convert all of the values I've gotten from the trig stuff and convert that into some sort of usable position.

However I'm still not entirely sure if I'm going about this in the correct way, I have attached a diagram, granted an bad one, but a diagram none the less of what I am trying to achieve. And also the code that I am using to try and achieve this. If anyone has any insight on this or could at least point me in the right direction that would be great. Thanks, Luke.

 RaycastHit hit;
             Vector3 rayOrigin = Vector3.zero;
             Vector3 oldCPos = Vector3.zero;
             Vector3 nextCPos = Vector3.zero;
             Quaternion oldRot;
 
             oldCPos = cTransform.localPosition;
             nextCPos = oldCPos + inputDirection;
             cTransform.localPosition = nextCPos;
 
             rayOrigin.x = mainCamera.transform.position.x;
             rayOrigin.y = weaponScript.transform.position.y;
             rayOrigin.z = mainCamera.transform.position.z;
 
             if (Physics.SphereCast(rayOrigin, 0.1F, weaponScript.transform.forward, out hit, weaponScript.clearDistance, weaponScript.noPlayerMask))
             {
                 hyp1 = Vector2.Distance(new Vector2(oldCPos.x, oldCPos.z), new Vector2(nextCPos.x, nextCPos.z));
                 angleA1 = Mathf.Acos(Mathf.Clamp(Vector3.Dot(transform.forward, hit.normal),-1.0F, 1.0F));
                 finalAdj = Mathf.Cos(angleA1) * hyp1;
                 hyp2 = Vector2.Distance(new Vector2(oldCPos.x + (cTransform.forward.x * weaponScript.clearDistance),
                     oldCPos.z + (cTransform.forward.z * weaponScript.clearDistance)), new Vector2(hit.point.x, hit.point.z));
                 angleA2 = angleA1;
                 finalOpp = Mathf.Sin(angleA2) * hyp2;
                 finalHyp = Mathf.Sqrt((finalAdj * finalAdj) + (finalOpp * finalOpp));
                 angleA3 = Mathf.Acos(finalAdj / finalHyp) * 57.29578F;
                 angleA1 *= 57.29578F;
                 finalAngleA = angleA1 - angleA3;
                 oldRot = cTransform.rotation;
 
                 cTransform.rotation = Quaternion.AngleAxis(finalAngleA, transform.forward);
 
                 inputDirection.x = cTransform.forward.x * finalHyp;
                 inputDirection.z = cTransform.forward.z * finalHyp;
 
                 cTransform.rotation = oldRot;
 }

alt text

help.jpg (358.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Physics.SphereCast please help understand 0 Answers

Lay a Sphere on a Point 3 Answers

Move object with mouse without passing through colliders 0 Answers

How do you perform a stationary SphereCastAll? 1 Answer

Is it possible to cast a ray but ignore Z position? 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