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 PsychoPsam · May 28, 2013 at 02:10 PM · raycastingflashlightcone

Rotating ray in cone shape needs to rotate with player - how?

I have this code which is to be used for a ray-casting test based on a flashlight. The ray spins around the edge of the flash light to determine where the edges intersect. It can also be used as a vision cone for security cameras when I get it to work.

I can get it to rotate around in a cone shape but the problem is, it always faces the same direction.

My question is, how do I get it to rotate with the players view? I figure it's one more rotation but I'm not sure how to add that.

Here's the code. You place the camera of the player in the Inspector as the source of the raycasting.

 using UnityEngine;
 using System.Collections;
 
 public class ConeRayTest : MonoBehaviour {
 
     public GameObject originObject;  // for example the camera.
     public float rayPitch = 40.0f;
     public float rayRotation = 1.0f;
     private GameObject tempObj;
         
     // Use this for initialization
     void Start () {
          tempObj = new GameObject("dummy"); 
     }
     
     // Update is called once per frame
     void Update () {
     
         tempObj.transform.position = originObject.transform.position;
         
         tempObj.transform.RotateAroundLocal(new Vector3(0,0,1), rayRotation * Time.deltaTime);
         
         tempObj.transform.position += (tempObj.transform.forward * 100);
         
         tempObj.transform.position += (tempObj.transform.up * rayPitch);
         
         Debug.DrawLine(originObject.transform.position, tempObj.transform.position);
     }
 }
 

   
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
0

Answer by robertbu · May 28, 2013 at 02:49 PM

The "one more rotation" is always the one that makes my brain bleed. Your way of walking points in a circle at a distance is interesting...not a way I'd of thought to solve this problem. Since you are using transform.forward, and transform.up, I think you can just do this on line 20:

 tempObj.transform.rotation = originObject.rotation;

If that does not work, here is an alternate approach to casting a cone:

 using UnityEngine;
 using System.Collections;
 
 public class ConeCast : MonoBehaviour {
     
     public Transform trTracker;  // Game object to track (i.e. camera)
     public float angle = 10.0f;  // Angle from forward to test
     public int tests = 20;       // Number of rays to cast
     
     private Transform trCenter;
     private Transform trRays;
     private float deltaAngle;
 
     void Start () {
         trCenter = new GameObject().transform;
         trRays = new GameObject().transform;
         trRays.parent = trCenter;
         trRays.Rotate(angle, 0.0f, 0.0f);
         deltaAngle = 360.0f / tests;
     }
     
     void Update () {
         trCenter.position = trTracker.position;
         trCenter.rotation = trTracker.rotation;
         
         for (int i = 0; i < tests; i++) {
             Debug.DrawRay (trCenter.position, trRays.forward * 20.0f, Color.green);    
             trCenter.Rotate (0.0f, 0.0f, deltaAngle);
         }
     }
 }

Comment
Add comment · Show 2 · 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 PsychoPsam · May 29, 2013 at 01:33 AM 0
Share

Thanks. That script is exactly what I want. I didn't think .Rotate would work that way.

avatar image robertbu · May 29, 2013 at 01:35 AM 0
Share

I'm not sure what you mean by "I don't thin Rotate would work that way." Rotate works because I've setup a couple of transforms with a parent and a child. The child transform is at an angle to the parent, and the code rotates the parent.

If your question is answered, please click the checkmark next to the answer to close it out.

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

Flashlight with fake bounce point light 1 Answer

Flashlight effect 2 Answers

Create interchanging Raycast cone for flashlight 2 Answers

Firing rays in a cone shape from the player 4 Answers

Multiple Camera.main.ScreenPointToRay 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