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 Showken · Oct 11, 2010 at 02:01 AM · cameragameobjectraycast

How can I check when the centre of camera is looking at a gameobject?

how to touch off a event when the centre of camrea look at a gameobject?

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

2 Replies

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

Answer by IJM · Oct 11, 2010 at 02:25 AM

I think that best way to do taht is to use raycasting.

Raycast script reference: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

Use ScreenToWorldPoint function to get the center of the camera. Your argument for ScreenToWorldPoint should be Vector3(Screen.width/2, Screen.heigh/2, camera.nearClipPlane).

ScreenToWorldPoint script reference: http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Example: (Attach it to an object that has a camera component)

using UnityEngine; using System.Collections;

public class example : MonoBehaviour { void Update() { Vector3 CameraCenter = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.heigh/2, camera.nearClipPlane)); if (Physics.Raycast(CameraCente, transform.forward, 100)) Debug.Log("Ou yeah!");

 }

}

p.s.

I didn't test this code ;)

Comment
Add comment · Show 3 · 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 Xitech_ · Apr 05, 2014 at 03:23 PM 0
Share

Thanks, works. However, how can I check wich object I hit? (I hope you're still active on the forum :p)

avatar image chillersanim · Apr 05, 2014 at 03:33 PM 1
Share

You can use the hit info.
In the given example, the hit info was not used, but you can add it like this:

 RaycastHit hit;
 var cameraCenter = camera.ScreenToWorldPoint(new Vector3(Screen.width / 2f, Screen.height / 2f, camera.nearClipPlane));
 if (Physics.Raycast(cameraCenter, this.transform.forward, out hit, 1000))
 {
     var obj = hit.transform.gameObject;
 }

The "obj" is the object that was hit.
If the code is not attached to your main camera, then use ins$$anonymous$$d of "this.transform.forward" the transform of your main camera "mainCamera.transform.forward".

Greetings Chillersanim

avatar image Xitech_ · Apr 05, 2014 at 03:41 PM 0
Share

Thank you very much. Im still not 100% familiar with raycasts. Cheers mate.

avatar image
0

Answer by Eoku · Oct 31, 2015 at 07:07 AM

As an alternative to the (Admittedly better) method presented above, you could just use the mouse. You would use Cursor.lockState = CursorLockMode.Locked to lock the cursor at the center of the screen and Cursor.visible = false to hide it. Then use any of the OnMouse... commands in MonoBehavior to make the object interactive. I guess this method would only be useful in certain circumstances (like the player picking up and carrying rigidbodies), but still worth a mention.

Cursor Info, MonoBehavior Info.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject Moving Slow when drag with hand 0 Answers

Some GameObjects can't touch !!! 0 Answers

Touch different Gameobjects using camera array and raycast hit 3 Answers

Ray Casting - Trigger function 1 Answer

Getting a camera at runtime 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