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 aidenkael · Nov 11, 2013 at 08:42 PM · raycastboo

Draw a ray to deterime if anything is in front of colliding object?

I have a security camera and a collider attached to the detection area. I want the script to detect that the colliding object is a player, and then using a raycast to determine if the player is behind one of the two ledges they can hide behind.

alt text

How can I do this? I believe I would have to find the position of the camera and for the origin point, but how can I determine the direction (the camera moves side to side 45 degrees) and length to send the ray?

My script is currently: import UnityEngine

 class detection_script (MonoBehaviour): 
 
     def Start ():
         pass
     
     def FixedUpdate ():
         pass
     
     def OnTriggerStay(collisionInfo as Collider):
         if collisionInfo.collider.tag == "Player":
unity assignmeny.png (381.2 kB)
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
0

Answer by jacobschellenberg · Nov 11, 2013 at 09:14 PM

Here is an example on the Unity references.

Attach a script to the camera itself and use the direction of the camera to determine where the Ray is pointing.

http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
     void Update() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, 100))
             Debug.DrawLine(ray.origin, hit.point);
         
     }
 }
Comment
Add comment · Show 19 · 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 aidenkael · Nov 11, 2013 at 09:19 PM 0
Share

Direction? Do you mean the rotation? That solves the origin point, but how do I know which direction to point the ray and at what distance? There is no mouse action in my project, the movement is done with the wasd keys.

avatar image jacobschellenberg · Nov 11, 2013 at 09:20 PM 0
Share

aye sorry. I believe Ray takes a vector3. So if the script is attached to the camera, then you could use Vector3.forward, and that will get the forward z direction of the camera.

avatar image jacobschellenberg · Nov 11, 2013 at 09:21 PM 0
Share

Here is the right one. It has the direction, Vector3.forward, the object hit, and the distance.

 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour {
     void Update() {
         RaycastHit hit;
         if (Physics.Raycast(transform.position, Vector3.forward, out hit, 100.0F))
             float distanceToGround = hit.distance;
         
     }
 }
avatar image aidenkael · Nov 11, 2013 at 09:23 PM 0
Share

Well I have a script attached to my camera, but the detection script is attached to the box collider. The box collider is a child of the camera and thats why it also rotates, but the detection script itself is not attached to the camera

avatar image jacobschellenberg · Nov 11, 2013 at 09:25 PM 0
Share

And the box collider is an empty object, or that is attached the camera? If it's an empty gameObject, just make sure it's forward is the same as the camera. Essentially zero out its z and you should be good.

Show more comments
avatar image
0
Wiki

Answer by Sekto · Nov 12, 2013 at 01:58 AM

 // C# Code
 

public float distance;

RaycastHit hit;

if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))

{

    distance = hit.distance;
     
    // now you now how far away you are from an object. With the hit variable you basically have a pointer to game object.

}

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Why Isn't My Raycast Working? 3 Answers

3rd Person camera collision 2 Answers

Melee-like function? 1 Answer

How To Invoke RayCastAll? 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