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 Joetjah · Jan 24, 2013 at 12:11 PM · cameraraycast

RayCasting from camera to player

I'm creating a game with a 3th person view. The camera follows the player throughout the map. But if I get to the very bottom of the map, the boundary (box) stops the player. The camera is following from a small distance, resulting the camera to be found at the other side of the wall (not seeing the player).

I'm trying to solve this problem by casting a Raycast to the Player GameObject. If the player is not seen, the camera should move along the y-axis (up) untill he sees the player again.

In the current code I have, I try to raycast and get information about the hitted object. The camera always looks at the player object. Even though, sometimes I get the message "Block" and sometimes the message "Plane", but never "Player", "Sphere" (which is the player's hull) or "Boundary". What is required for the raycast to see/hit them?

Current code:

 var hit: RaycastHit;
 var ray: Ray = camera.ScreenPointToRay(target.transform.position);
 if (Physics.Raycast(ray, hit)) {
     var objectHit: Transform = hit.transform;
     Debug.Log(objectHit);
 }
Comment
Add comment · Show 1
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 TSRajesh · Aug 25, 2013 at 01:40 PM 0
Share

In this case, Linecast would be a better candidate.

2 Replies

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

Answer by Nanorock · Jan 24, 2013 at 12:56 PM

ScreenPointToRay needs a Screen position, not the target transform. http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html

I would try :

 var screenPos : Vector3 = camera.WorldToScreenPoint (target.transform.position);
 var ray: Ray = camera.ScreenPointToRay(screenPos);
 Debug.DrawRay (ray.origin, ray.direction *  50, Color.yellow);
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 KoningStoma · Jan 24, 2013 at 12:40 PM

I would do it the other way around.

Cast a ray from the player in the direction of the camera. If the ray hits a wall that's closer to the player than the preferred camera distance, move the camera towards the player.

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

13 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

Related Questions

Problem With Raycasts, Interaction Script. 1 Answer

A node in a childnode? 1 Answer

How do I check if the mouse is over a VISIBLE vertex? 1 Answer

Raycast From Camera to Object 1 Answer

Detect touched 2d object in Unity3d 4.3 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