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 Lairinus · Apr 06, 2013 at 03:33 AM · c#raycastingpathing

Managing Raycast Rotations

Hello,

What I'm trying to do is make a sort of Pathing algorithm with using forward/diagonal rays.

Right now my code is : (OLD CODE, working code below)

     void managePathingAndMovement()
     {
         forwardRayCast();
         if (!moveTowardsEnd)
         {
             translate();
         }
     }
     
     private Vector3 moveDirection; //No point in using atm
     void forwardRayCast()
     {
         Ray fowRay = new Ray(_rayCastObject.position, Vector3.forward);
         RaycastHit fowHit;
         if (Physics.Raycast(fowRay, out fowHit, Mathf.Infinity))                                 //Change Mathf.Inf
         {
             transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 1, 0);  //Sets the y rotation to +1 every frame while stil Raycasting
         }
         else
         {
             moveTowardsEnd = true;
         }
     }
     
     void translate()
     {
         transform.Translate(Vector3.forward * 10 * Time.deltaTime);
     }

I can't actually rotate the transform itself, and the weird thing is that my Vector3.forward changes as my character rotates, but the Raycast seems to be working independently.

Also, I've tried shooting the ray from both the transform itself and the child object. Both times it's just pointing dead ahead. How would I go about fixing this? -- Fixed with multiplication :(

     private Vector3 moveDirection;
     void forwardRayCast()
     {
         moveTowardsEnd = false;
         Vector3 forward = Quaternion.Euler(_rayCastObject.localEulerAngles) * Vector3.forward;    //Rotates toward the localRotation by using eulerAngles
         Ray fowRay = new Ray(_rayCastObject.position,forward );
         RaycastHit fowHit;
         Debug.DrawRay(_rayCastObject.position, forward);
         if (Physics.Raycast(fowRay, out fowHit, Mathf.Infinity))                                 //Change Mathf.Inf
         {
             transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 1, 0);  //Sets the y rotation to +1 every frame while stil Raycasting
             _rayCastObject.localEulerAngles = transform.localEulerAngles;
         }
         else
         {
             //moveTowardsEnd = true;
         }
     }

 
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 Chronos-L · Apr 06, 2013 at 05:30 AM

In your case, You should use transform.forward, transform.right, and transform.up in oppose to Vector3.up etc.

Vector3.forward is a shorthand of (0, 0, 1). Your Vector3.forward doesn't change, when you use:

 transform.Translate(Vector3.forward * 10 * Time.deltaTime);

what happened is that it will move your transform by Vector3.forward at your transform's local space. For raycast, you need to work with world space.

If you want to raycast in front of your character/gun/AI, you should use:

 Ray fowRay = new Ray(_rayCastObject.position, _rayCastObject.forward);

fowRay will be pointing at the same direction the _rayCastObject's forward direction is.

If you need to look at other angles other that the 3 axis, you can use:

 Vector3 lookAtheGroundInFront = transformObjectThatIsLooking.TranslateDirection( new Vector3( 0, -1, 1 ) );

You can either normalize or not normalize the vector, it is up to you and your calculation.

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 Lairinus · Apr 06, 2013 at 05:43 AM 0
Share

Thanks for the response.

I literally just got done doing some multiplication (after adding the vectors for a few hours) and I came up with this:

 Vector3 forward = Quaternion.Euler(_rayCastObject.localEulerAngles) * Vector3.forward;

What that does is it keeps my "local" forward the same based on the character's rotation. To be sure of this, I also set the eulerAngles from the transform and the rayObject(transform) equal to each other.

The only time it seems I come to an answer is after I spent hours trying to come up with it, and always after someone posts one lol

avatar image Chronos-L · Apr 06, 2013 at 05:49 AM 0
Share

I am glad that you find a way around your problem, just to inform you

 Vector3 forward = Quaternion.Euler(_rayCastObject.localEulerAngles) * Vector3.forward;

the computed forward is equivalent to transform.forward.

avatar image Lairinus · Apr 06, 2013 at 05:59 AM 0
Share

......

derp on my part lol. thanks for pointing that out.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[C#] UNET Client raycast not the same as Server raycast 2 Answers

Using raycast for visible bullet collision (not oncollisionenter) in C# 1 Answer

How can I have separate floats for each gameobject? 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