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 Razputin · Apr 15, 2021 at 03:30 PM · raycastvector3movedirection

Raycast in Direction of Input Movement

I have a vector3 MoveDirection X = Horizontal Input Z = Vertical Input

I want to fire a ray, from the player towards the direction the player is moving. Then if this ray hits the "Move Toward" object I can return true.

Possible example

    Vector3 v = new Vector3(moveDirection.x, 0, moveDirection.z);
 
         RaycastHit hit;
         if (Physics.Raycast(player.transform.position, v, out hit, 5)
         {
             if(hit.collider.tag == "ObjectWeWantToMoveToward")
             {
                 //we must be moving toward it as our move direction ray is pointing at it
                 return true;
             }
             else
             {
                 //we must not be moving toward it as our move direction ray is not pointing towards it
                 return false;
             }
         }

The obvious issue here is that our vector3 of V is not actually the direction we want to point the ray to. So how do I properly fire the ray in the correct direction?

Thanks!

Comment
Add comment · Show 4
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 jackmw94 · Apr 16, 2021 at 09:48 AM 0
Share

Nothing stands out in this code as problematic, I am not sure why v wouldn't be the direction you're moving.

My only suggestions are could the move direction you're receiving be local to your player object, if your player is a child of another object and this movement sets its LOCAL position then this could be the case and to get the global space move direction then you'll want to use the "TransformDirection" function on your player's transform: v = player.transform.TransformDirection(v);

If you player always faces the direction they're facing you could use player.transform.forward as your ray direction?

I know comparing strings can have certain caveats where two strings of equal content do not return true when compared with the == operator, so use .Equals or in this case you can use the CompareTag function on gameobject: hit.collider.gameObject.CompareTag("ObjectWeWantToMoveForward")

These doesn't seem too likely though. To debug I'd really recommend using Gizmos as it's super hard to tell what's happening in 3D space from just logs of Vector3s. If you haven't used them, gizmos get drawn in the OnDrawGizmos function:

 private void OnDrawGizmos()
 {
     Gizmos.color = Color.magenta;
     Vector3 v = new Vector3(moveDirection.x, 0f, moveDirection.z);
     Gizmos.DrawRay(player.transform.position, v);
 }


Then in the scene view you'll be able to see the direction of your ray as a magenta line, then move your player and see if you can see any patterns in the line's behaviour.

Let me know if I've misunderstood anything about your question! If none of this is helpful then post the code where you apply that movement to your player's transform.

avatar image Razputin jackmw94 · Apr 16, 2021 at 12:12 PM 0
Share

The game is an FPS so x/z will not always be the same direction, as the player can rotate. X/Z in Movement will always be -1, or 1. As they're inputs.

Currently I'm firing a ray toward the rigidbody velocity, as this is the correct direction we are going. However since my velocity basically stops when I hit a wall. this solution won't work.

avatar image Razputin Razputin · Apr 16, 2021 at 12:58 PM 0
Share

I figured it out. I was calling the wrong transform, as this script is in a third party.... my mistake. The standard transform.TransformDirection solution works.

avatar image andrew-lukasik · Apr 16, 2021 at 12:57 PM 0
Share

When in doubt, visualize your raycasts.

 if( moveDirection.sqrMagnitude>0 )// prevents raycast when stationary
 {
     const int physicsLayerMask = 1<<0;// 1<<0 means Default layer only
     const float rayMaxDistance = 5f;
 
     var ray = new Ray{
         origin      = player.transform.position ,
         direction   = new Vector3{ x=moveDirection.x , z=moveDirection.z }
     };
     if( Physics.Raycast( ray , out var hit , maxDistance:rayMaxDistance , layerMask:physicsLayerMask ) )
     {
         // hit
         Debug.DrawLine( ray.origin , ray.origin + ray.direction.normalized*hit.distance , Color.red );
     }
     else
     {
         // miss
         Debug.DrawRay( ray.origin , ray.direction , Color.yellow );
     }
 }

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

185 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Why multiply a vector by -2? 1 Answer

Shoot Raycast to a specified Angle 0 Answers

How to get an offset of a RaycastHit? 0 Answers

raycast and trigonometry 2 Answers

Shotting Or thrwoing Object wtih Vector3.MoveTowards 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