- Home /
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);
}
}
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);
}
}
}
Thanks. That script is exactly what I want. I didn't think .Rotate would work that way.
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
Follow this Question
Related Questions
Flashlight with fake bounce point light 1 Answer
Flashlight effect 2 Answers
Create interchanging Raycast cone for flashlight 2 Answers