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 Levantez · Mar 04, 2014 at 05:01 AM · physicsraycastcalculationshit detection

Raycast, Calculate Random Direction

Hi,

There is a Vector3 Position, and there is a target.

I need to Raycast from the position to target at a deviated Random Angle.

However, the Script below gave me Raycasted Random Square Area instead of a Circle.

Could someone guide me in how to get the Random Raycast to be inside the Circle?

alt text

 Vector3 CalcBulletDirectionVec3( Transform attackTransform )
     {
         Vector3 targetTransformPos = GetTargetTransform().position;
         Vector3 tempDir = targetTransformPos - attackTransform.position;
 
         tempDir = tempDir.normalized;
 
         tempDir = Quaternion.Euler(CalcRandomAngleRange()) * tempDir;
 
         return tempDir;
     }

 Vector3 CalcRandomAngleRange()
     {
         float maxAngle = CalcMaxAngle();
         return new Vector3(GetRandomRange(maxAngle), GetRandomRange(maxAngle), 0.0f);
     }
 
     float GetRandomRange( float maxAngle )
     {
         return Random.Range(-maxAngle, maxAngle);
     }


untitled-1.jpg (63.1 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

1 Reply

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

Answer by robertbu · Mar 04, 2014 at 08:26 AM

For a circle like you want, it is easier (for me anyway) to think of of the problem as having a maximum inaccuracy at a specified distance.

 Vector3 CalcBulletDirectionVec3( Transform attackTransform )
 {
    Vector3 targetTransformPos = GetTargetTransform().position;
    Vector3 tempDir = targetTransformPos - attackTransform.position;
 
    tempDir = tempDir.normalized;

    Vector3 crossDir = Vector3.Cross(tempDir, Vector3.up);
    crossDir = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), tempDir) * crossDir;
    crossDir = Random.Range(0.0f, maxDelta) * crossDir;
    tempDir += crossDir;
 
    return tempDir;
 }

'maxDelta' is the maximum amount from a perfect shot the ray will be at one unit from the origin of the vector. This code depends on having one vector that is not parallel to the 'tempDir' vector for use in the Vector3.Cross(). I've selected Vector3.up because usually games either don't provide shooting directly up or shooting directly up is meaningless.

The way this code works is to first find a vector that is orthogonal to (at right angles to) 'tempDir'. It then picks a random value between 0.0 and 360.0 and swings that vector around using 'tempDir' as the axis of rotation. Then it scales the vector between 0.0 and maxDelta. The result is then used to offset 'tempDir'.

If you really want angles for some reason, you could use an AngleAxis rotations to swing the vector left/right and up/down, but the result would be square, not a circle.

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 Levantez · Mar 04, 2014 at 07:15 PM 0
Share

Thank you for your help Robertbu,

I understand the logic, but this line seems to produce an error at the moment, and I'm finding an alternative to it.

 crossDir = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), tempDir);
avatar image robertbu · Mar 04, 2014 at 07:21 PM 0
Share

Sorry about that. I fixed the code in the answer. The correct line is:

  crossDir = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), tempDir) * crossDir;
avatar image Levantez · Mar 04, 2014 at 07:29 PM 0
Share

I got it working now, Thank you

 crossDir = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), tempDir) * crossDir;

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

Debug.DrawLine problem 1 Answer

Having a RaycastHit event create a component? 1 Answer

Problem With RayCast 3 Answers

thousands of raycasts in nested for loop 0 Answers

Need help with Raycasting 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