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 xwolfhunter · Jun 29, 2012 at 09:05 PM · rotationraycastdirection

Making My Raycast Face The Right Direction

I am currently attempting to create a sentry-type enemy for my (top-down) game. What I want it to do is have four raycasts - one on its left side, right side, forward side, and back side from a top-down view. If these raycasts are triggered, it is to fire a bullet.

I have all of this under control, save one thing: How am I supposed to make the raycast face the desired direction?

Here's my code:

     var hit : RaycastHit;
 public var rightBullet = gameObject;
 
 function Update () {
     if(Physics.Raycast(transform.position,transform.rotation.y,hit)){
     Instantiate(rightBullet,transform.position,transform.rotation, 7);
     }
 }



What I can't seem to figure out is how to get the transform.rotation.y to be 90, -90, 180, and 0. To clarify a possibly murky misinterpret point, I plan on writing four scripte - one for each raycast.

I have never done anything with raycasts before (Heck, this is day four of my first game), so any help would be appreciated. Thanks!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Piflik · Jun 29, 2012 at 09:11 PM

You don't have to rotate the object to shoot a ray in a specified direction.

 (-)transform.right
 (-)transform.forward
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
avatar image
1

Answer by aldonaletto · Jun 30, 2012 at 12:48 AM

The second parameter in Physics.Raycast is wrong - it should be a vector pointing the raycast direction. You could do the following:

var rightBullet: GameObject; // drag the bullet prefab here var bulletSpeed: float = 20; private var hit : RaycastHit;

// this function does the raycast and shoots if something is there

function RaycastAndShoot(dir: Vector3){ // if raycast in the specified dir hits something... if (Physics.Raycast(transform.position, dir, hit)){ // create the bullet rotated to the specified direction Instantiate(rightBullet, transform.position, Quaternion.LookRotation(dir)); // accelerate the bullet in the specified direction rightBullet.velocity = dir * bulletSpeed; } }

function Update () { RaycastAndShoot(transform.forward); RaycastAndShoot(transform.right); RaycastAndShoot(-transform.forward); // equivalent to "transform.backward" (doesn't exist) RaycastAndShoot(-transform.right); // equivalent to "transform.left" (doesn't exist) }

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

Answer by Wolfram · Jun 29, 2012 at 09:17 PM

Don't use individual components of transform.rotation, they have nothing to do with axes, they are part of an internal Quaterion representation. Read the docs.

Furthermore, transform.rotation.y would have been a float, while a direction must be a Vector3. I stronly recommend to add "#pragma strict" as the first line to every .js script you work with. This will produce compiler errors which will tell you that something is wrong.

To actually fix your problem, use transform.right and transform.forward, wich will be the Vector3 directions of your object's local X and Z axis. For example, -transform.up will be -Y.

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 xwolfhunter · Jun 29, 2012 at 11:59 PM 0
Share

Thank you.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rotating ray with player rotation. 2 Answers

Raycasting from object rotation? 1 Answer

How to incorporate a rotation towards mouse position in this script? I tried 0 Answers

C# Raycast from Object direction (z-axis) + another Vector3 1 Answer

How to make an object rotate to raycast hit from mouse? Unity 3D 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