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 /
This question was closed Jul 22, 2013 at 03:55 PM by MadJohny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MadJohny · May 10, 2013 at 11:05 AM · camerajavascriptraycastvector3

i don't understand Vector3.forward correctly on camera

Hi, I am trying to get a raycast from the camera to forward, but it's not working correclty, it's pointing forward in the world space and not in camera local position. I really don't know what to say more about this problem, but I'll leave you the scripts for the camera and the one I'm trying to raycast from. Oh, and I'm not using a regular character controller, I'm using a rigidbody and also using a different script for the camera. this is the script where I'm trying to raycast:

 #pragma strict
 var range : float;
 
 var bombs : int = 1;
 var bomb : Transform;
 
 function Update () 
 {
     Debug.DrawRay(transform.position, Vector3.forward * range, Color.red);
     
     if (Input.GetMouseButtonDown(0) && bombs >=1)
     {
         var Hit : RaycastHit;
         if (Physics.Raycast(transform.position, Vector3.forward , Hit, range))
         {
             if(Hit.collider.gameObject.tag == "Ground")
             {
             Debug.Log ("Found Ground");
             var bomb : Transform = Instantiate (bomb, Hit.collider.gameObject.transform.position, Quaternion.identity);
             }
         }
     }
 }

And here is the camera script:

 #pragma strict
 
 var lookSensivity : float = 5;
 @HideInInspector
 var yRotation : float;
 @HideInInspector
 var xRotation : float;
 @HideInInspector
 var currentYRotation : float;
 @HideInInspector
 var currentXRotation : float;
 @HideInInspector
 var yRotationV : float;
 @HideInInspector
 var xRotationV : float;
 var lookSmoothDamp : float = 0.1;
 
 function Start () {
     Screen.lockCursor = true;
 }
 
 function Update () 
 {
     yRotation += Input.GetAxis("Mouse X") * lookSensivity;
     xRotation -= Input.GetAxis("Mouse Y") * lookSensivity;
     
     xRotation = Mathf.Clamp(xRotation, -90, 90);
     
     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
     
     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
     
     if (Input.GetButtonDown("Esc"))
     Screen.lockCursor = false;
     if (Input.GetButtonDown("Fire1"))
     Screen.lockCursor = true;
 
 }
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 robertbu · May 10, 2013 at 11:26 AM 0
Share

I don't know what you mean by this phrase: "raycast from the camera to forward". Typically you would want to raycast from the forward of the object using 'transform.forward.'

 Debug.DrawRay(transform.position, transform.forward * range, Color.red);

If for some reason you want the camera forward, you can use 'Camera.main.transform.forward'.

avatar image MadJohny · May 10, 2013 at 11:32 AM 0
Share

Sorry for my english, I want to raycast from the forward of the object, can you try using the scripts in a roject? add a camera, put the camera script on it, then put the bombplacer script on it, the raycast is not pointing correctly

avatar image robertbu · May 10, 2013 at 01:57 PM 0
Share

Your code above use 'Vector3.forward'. You want 'transform.forward' if you want the forward of the object.

avatar image MadJohny · May 10, 2013 at 02:07 PM 0
Share

Yes and that's what I wanted

1 Reply

  • Sort: 
avatar image
3

Answer by Vonni · May 10, 2013 at 02:04 PM

Vector3.forward is the same as Vector3(0,0,1), its just an abreviation. BUT, when you use transform.forward, it takes the direction of the local Z-axis on the object. More importantly, try to print values when you can, or when things aren't working as expected. Like: print(Vector3.forward). Always use these tests to find out where things are going wrong.

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

Follow this Question

Answers Answers and Comments

15 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

Related Questions

cast a ray to hit an object in the center of the screen 1 Answer

How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers

Camera Zoom in&out 2 Answers

Natural camera sway 1 Answer

Ray through camera 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