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 alexanderameye · Dec 21, 2014 at 10:29 PM · cameraraycastraycasthitcursor

Raycasting with the origin of the ray coming from the cursor

Hello, I have a player with a movement script attached to it, a camera child to the player with a mouselook script attached to it.

Then I attached this raycasting script to it, I want the ray to move when I move the camera with the mouselook script, so that the ray basically has its origin point in the center of the screen, and the direction changes when the camera changes.

 using UnityEngine;
 using System.Collections;
 
 public class gg : MonoBehaviour {
 
     public float distance = 5;
 
     // Use this for initialization
     void Start () {
 
 
     
     }
     
     void Update() 
     {
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast (ray, distance)) 
         {
             print ("Yes");
         } 
 
         else 
         {
             print ("No");
         }
 
         Debug.DrawRay (ray.origin, ray.direction * distance, Color.red);
     }
 }

I get this error: 'NullReferenceException: Object reference not set to an instance of an object' on line 18, (Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);)

Tysm for helping!

Comment
Add comment · Show 3
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 alexanderameye · Dec 21, 2014 at 10:32 PM 0
Share

$$anonymous$$ight be a noob question, do I need to declare a camera variable first?

avatar image Alec-Slayden · Dec 21, 2014 at 10:33 PM 1
Share

Looks ok to me... check to make sure you have a camera with the $$anonymous$$ainCamera tag.

Camera.main is a shortcut to finding an existing camera that has the $$anonymous$$ainCamera tag. If there isn't one, or if it's not active, it will throw a null reference

avatar image alexanderameye · Dec 21, 2014 at 10:40 PM 0
Share

this kind of solved it, only thing is, the rays direction follows my cursor's position, so it's kind of weird right now, could I just solve this by locking the cursor in the middle of the screen and making it invisible? because I do have an ai$$anonymous$$g icon I placed in the middle of the screen

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Alec-Slayden · Dec 21, 2014 at 11:25 PM

You probably don't need to use the mouse position if your ray is always coming from your view center.

Consider building your ray like this:

     Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f) );

This sets the origin to be the center of the camera's view, outward.

Then, as long as you have a main camera, and as long as your distance in the raycast is enough, you shouldn't have any problems.

EDIT: I may have the wrong idea about what you are trying to do. Your original script causes a ray to come from the centerpoint of the camera down the mouse position into the world, and will have an angled effect like looking down the ray as a barrel, so it won't be visible. If you want the ray to come directly from the center at all times, the mouse position isn't necessary, but the ray will still not be visible to the camera (still looking down it).

Alternative rays include casting from a world mouse position, to a position using the camera's forward axis. This is not often used, in my experience.:

 Vector3 mouseScreen = Input.mousePosition;
             // you must define a starting plane away from the camera for this to work.
             // this essentially specifies the 'radius' of the mouse's influence.
         mouseScreen.z = 0.5f;  
         Vector3 rayOrigin =  Camera.main.ScreenToWorldPoint( mouseScreen );

             // use the camera's forward direction
         Vector3 rayDirection = Camera.main.transform.TransformDirection(Vector3.forward);


This will cause rays to follow the mouse but always be parallel to the camera's z axis.

If you are instead looking for a ray from the camera's center to a specific world depth, instead of looking down a ray like your original code, you need to specify a world position for the ray's end point.

 Vector3 mouseScreen = Input.mousePosition;
         mouseScreen.z = 0.5f;
         Vector3 rayDest = Camera.main.ScreenToWorldPoint( mouseScreen);
         
         // ray starts at camera center
         Vector3 rayOrigin =  Camera.main.transform.position;
         
         // ray goes to the mouse position a specific distance from the camera.
         Vector3 rayDirection = rayDest - rayOrigin;
 
         Ray ray = new Ray(rayOrigin, rayDirection);

This is another less usual version, where you are specifying a second point in space to draw the line to from the camera's center, based on the position of the mouse.

Hopefully one of these is what you're looking for.

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 alexanderameye · Dec 22, 2014 at 09:30 AM

The first script did exactly what I needed, ty! I also understand now why I need to use 'ViewportPointToRay' instead of 'ScreenPointToRay'. Ty man!

Alexander

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

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

27 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

Related Questions

How can I change this script? 0 Answers

RE: RaycastHit and camera through walls 0 Answers

Move toward Mouse Cursor (RaycastHit), without following? 1 Answer

How to detect UI Elements with Raycast 1 Answer

Raycast hitting objects to the left of my player 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