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 017Bluefield · May 27, 2015 at 06:39 AM · c#cameraraycastcamera-lookaiming

Using raycast from camera to aim shots

I'm trying to make the player camera use a raycast that aims at the center of the screen. I'm trying to have one of my weapons aim at the raycast hit on its own so that, when I press the mouse button to shoot this weapon, it targets the raycast hit.

However, I've run into some problems. One of these problems involves the following line of C# code:

 Ray ray = camera.ScreenPointToRay(Input.mousePosition);

The issue stems from the "ScreenPointToRay" portion of the code. When I copied and pasted the code into MonoDevelop, this very part of the script displays in red. Is something wrong with the code? Is the documentation somehow incorrect?

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 KdRWaylander · May 27, 2015 at 07:50 AM

Hi,

If it's red it means that 'ScreenPointToRay' is not a member of 'camera'. Since your code seems to be good, the problem must come from 'camera'. Try replacing 'camera' by 'Camera.main' (which returns the camera tagged 'Main Camera') ? Is 'camera' the gameobject or the camera component ?

Comment
Add comment · Show 6 · 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 017Bluefield · May 31, 2015 at 05:02 AM 0
Share

It's the camera itself. I don't think I've heard of the "camera component".

P.S.: what is "Camera.main"?

P.S.S.: the code below is what first brings up the player's camera in the script:

 public Camera playerCamera;
avatar image KdRWaylander · Jun 01, 2015 at 06:54 AM 1
Share

Camera.main is a piece of code that will always return the camera with the "$$anonymous$$ain Camera" tag. It's like GameObject.Find("$$anonymous$$ain Camera") !

Alright so you do take the component, try changing the type to

 public GameObject playerCamera;
avatar image Eno-Khaon · Jun 01, 2015 at 07:01 AM 1
Share

If you are, in fact, declaring the Camera using

 public Camera playerCamera;

and calling it using

 Ray ray = camera.ScreenPointToRay(Input.mousePosition);

then that's a big part of the problem right there.

"camera" is something you can jump to/between, like "gameObject" and "transform" (i.e. gameObject.transform.position). Your variable is "playerCamera" which means you're not actually calling it. Ins$$anonymous$$d, you're calling the camera component of the object the script is attached to, whether it has one or not.

avatar image 017Bluefield · Jun 02, 2015 at 07:08 AM 0
Share

Okay, but now this brings up another problem. As soon as I change "Camera" to "GameObject", the ".ScreenPointToRay" portion of the code turns red. Is there an alternative to this code piece?

avatar image KdRWaylander · Jun 02, 2015 at 07:51 AM 0
Share

Alright,

Try calling the camera with the type GameObject (`public GameObject playerCamera;`)

Then, in your ray, call the camera component of the object:

 Ray ray = playerCamera.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);

Show more comments

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

19 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

Related Questions

Raycast hitting objects to the left of my player 1 Answer

Help My camera rotation script when a player hits a wall or game object it goes crazy. How can i change it to prevent this. 0 Answers

Raycast in camera 1 Answer

Raycasting with Rotation of Camera 0 Answers

How to make my Camera Controller look at target 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