• 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 hardmode2236 · Aug 30, 2013 at 12:29 AM · javascriptphysicspositionvector3spherecast

JS access spherecast position

In t$$anonymous$$s 2d concept im trying to make a bacteria colony produce spores. So i make it look in a random direction around itself and if it casts a sphere in that direction without $$anonymous$$tting any other spores, then it grows a spore there. But, im not sure how to re-access the spherecast's position once iv used it. here is my code:

 #pragma strict
 var growthCount = 0;
 var growthCountMax: int;
 var newSpore: GameObject;
 var randomDir: Vector3;
 var $$anonymous$$t: RaycastHit;
 var spawnDistMaster: float;
 var spawnDist: float;
 var spawnDistNoise: float;
 var newSporePos: Vector3;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
     
 }
 
 function incGrowthCount(num:int)
 {
     growthCount = num-1;
 }
 
 function lookForGrowSpot()
 {
     spawnDist = spawnDistMaster + Random.Range(-spawnDistNoise, spawnDistNoise);
     randomDir = Vector3(Random.Range(-359,359),0,Random.Range(-359,359));
     Debug.DrawRay(transform.position, randomDir, Color.cyan);
     
     if(!Physics.SphereCast(transform.position, .3, randomDir, $$anonymous$$t, spawnDist))
     {
         Debug.Log("Hit Not$$anonymous$$ng");
         //store the spherecasts ending position in "newSporePos"
         //newSporePos = ?;
         grow();
         return true;
     }
     Debug.Log("$$anonymous$$t somt$$anonymous$$ng");
     return false;
 }
 
 function grow()
 {
     //make spore at newSporePos
 }

also as a bonus question: is there a better way to make a random direction in 2d than what i did? what i have now just feels wrong.

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 aldonaletto · Aug 30, 2013 at 01:14 AM

The last will be first: you can generate a vector pointing in a random direction inside the plane XZ with a code like t$$anonymous$$s:

 randomDir = Quaternion.Euler(0, Random.Range(0, 360), 0) * Vector3.forward;

It generates a random rotation from 0 to 359 and applies it to a unit vector that originally pointed in the Z axis direction.

About the SphereCast: you should calculate the position by adding the random direction vector to the origin - but make sure that the vector length is equal to the distance to the new spore - it will be easy if randomDir is generated like above, because it has unit length: just multiply it by the desired distance. The code could be like t$$anonymous$$s:

 ...
 function lookForGrowSpot()
 {
     // generate unit vector pointing in a random direction:
     randomDir = Quaternion.Euler(0, Random.Range(0, 360), 0) * Vector3.forward;
     Debug.DrawRay(transform.position, randomDir, Color.cyan);
     // if position free...
     if(!Physics.SphereCast(transform.position, .3, randomDir, $$anonymous$$t, spawnDist))
     {  // calculate the position:
        newSporePos = transform.position + spawnDist * randomDir;
        grow();
        return true;
     }
     return false;
 }
  

NOTE: The spores must have colliders in order to be detected by SphereCast.

Comment
Add comment · Show 1 · 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 hardmode2236 · Aug 30, 2013 at 01:20 AM 0
Share

ok i see now. thanks for the help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer

Vector3.lerp doesn't work 1 Answer

Instantiated object isnt moving 1 Answer

Vector3 Transform.Position Not Working 2 Answers

instantiate an object on the intersection point between two objects on the x and y axis 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