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 nipundavid · Sep 18, 2013 at 12:49 PM · raycastangle

How to cast ray from a object in direction local to that object?

Hi, I m totally new to unity. I m trying to build AI in car game, I am firing a ray from enemy car and checking whether there is a object or not.

 Ray ray = new Ray(transform.position,transform.forward);
         RaycastHit hit;
         Debug.DrawRay(transform.position,transform.forward);
         if (Physics.Raycast(ray,out hit,rayDistance)) {
             if(hit.collider.tag == "rail") {
                 Debug.Log("AI working");
                 transform.Rotate(Vector3.up, 100*speed * Time.deltaTime);
             }
         }
         transform.Translate(0,0,speed);

This thing is working. but

Now i am not able to understand how to cast a ray at an angle of 45 degrees to car not world space so that when car take turn the ray still be at 45 degrees to the car.

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

Answer by meat5000 · Sep 18, 2013 at 12:58 PM

Check out the Transform docs.

Specifically look at the functions:

TransformDirection, TransformPoint, InverseTransformDirection, InverseTransformPoint.

Use these functions to convert from Local/World spaces to the other.

Something to consider : If Z is your forward axis, Vector3(1,0,1) would represent a 45 degree angle, forward/right.

Perhaps your could have a child object on the car perform a ray. As the child transform is relative to the parent it will have fixed position to the car.

I don't want to post a load of code as, from personal experience, it is better to work thee functions out yourself to get to grips with how they work. Have a go, come back if you want further help :)

Edit: You can probably use Vector3.Project to achieve your effect.

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 matejaizverieas · Oct 21, 2020 at 05:46 PM 0
Share

even thou this post is OLD. I thank you for fixing my problem, TransformPoint was the one that I needed

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

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

How to detect object by rotations-raycast? 0 Answers

How to shoot raycast with slight random direction? 1 Answer

Angle between Ray and Normal 1 Answer

Why does the raycast hit only objects/faces of a certain rotation? 1 Answer

Making an object follow the mouse in 3D world 0 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