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 BeforeChaos · Jul 09, 2012 at 09:59 AM · vector3localworld

Determining a vector3 at a random point along the local x axis of a gameObject

This is for a AI script I am trying to put together for fun. Yeah I am a total noob, only been coding for a couple of weeks, but I'm already biting off more than I can chew, so what yawannafightaboutit? :P

Anyway,

  • Bot looks forward, throws out a raycast along transform.forward;

  • Registers the hitpoint.

  • Determines the distance of the raycast, and divides it by 50.

  • Determines a random point along that line, no closer than 2/5 of the distance, no further than 4/5 of the distance.

  • Creates a Vector3 at that point, and remembers it in a private variable, to be used for a variety of things (movement, awareness, object creation, various other shenanigans).

Here is the code I am currently using, and please excuse the spaghettist nature, I can up some cleaner code if this is confusing. The function is isolated from "void Update" so that it only needs to be called when the AI needs it.

 void Check ()
 {
     RaycastHit hit; //in the right spot?
     float lookFactor;
     //Vector3 tooFar;
     Vector3 lookSpot;
     if (Physics.Raycast(transform.position, myTransform.forward, out hit))
     {
         safeDistance = hit.distance;//this keeps the safeDistance variable updated
         lookFactor = hit.distance / 50F;
         //tooFar = hit.point;
         lookSpot = new Vector3(Random.Range((lookFactor * 10F), (lookFactor * 40F)), 0, 0);
         //lookSpot = (tooNear.x + Random.Range((lookFactor * 10F), (lookFactor * 40F)));
         //tooNear.x += Random.Range((lookFactor * 10F), (lookFactor * 40F));
         if (!lookSpotted)
         {
             Instantiate(lookTarget, lookSpot, transform.rotation);
             lookSpotted = true;
         }
         //Instantiate(lookTarget, lookSpot, transform.rotation);
 
         // get this object's position
         //pos.x += Random.Range(-range, range); // add a random x offset around it
         //var instance : GameObject = Instantiate(thePrefab, pos, transform.rotation);
     }
     if (safeDistance <= (pathDistance))
     {
         kindaStuck = true;
         //Debug.Log ("Kinda Stuck");//enemy is kinda stuck
     }
     else
     {
         kindaStuck = false;
         //Debug.Log ("Trippin you out!"); //enemy is free to move
     }
 }

Currently I am creating a gameObject at the relevant position, but only according to world space not local space. If I can get it to determine the point according to the transform's local space instead, then I am winning.

Help!

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

Answer by Bunny83 · Jul 09, 2012 at 11:05 AM

I'm a bit confused about your directions. You raycast along the local forward axis (so the z-axis). In the description you say you want the point along that line, and not shifted to the left or right... Next thing is you should work with local vectors and don't fiddle around with world-space positions and change them in one world space axis.

If you really just want what you described in your few points above, just do it like this:

 Vector3 lookSpot;
 
 void Check ()
 {
     RaycastHit hit;
     if (Physics.Raycast(myTransform.position, myTransform.forward, out hit))
     {
         Vector3 direction = (hit.point - myTransform.position) / 50.0f;
         lookSpot = direction * (Random.Range(2.0f,4.0f) / 5.0f);
         // [...]
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding Position based on a local perspective 1 Answer

Vertices Array only given in local coords? 1 Answer

How do I make rigidbody.velocity in local space? 1 Answer

an ignorant's trigonometry woes: rotate a transform around an axis 2 Answers

How do I center a child object of camera on other object's screen position? 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