I need a raycast to come out from the center of my camera out into the world and stop after a certain limit. I've never messed with raycasts before and still trying to learn 3D programming, so this is kind of confusing to me. I currently have some script but it's not working very well for me, but here is my current code.
using UnityEngine;
using System.Collections;
public class Raycast : MonoBehaviour {
Ray RayOrigin;
RaycastHit HitInfo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.E)){
RayOrigin = Camera.main.ViewportPointToRay(new Vector3(0,0,0));
if (Physics.Raycast(RayOrigin,out HitInfo,100f)){
Debug.DrawRay(RayOrigin.direction,HitInfo.point,Color.yellow);
}
}
}
}
At this moment it's creating a raycast on the edge of my camera's view and putting it to the same place on the map, no matter where I move the camera. Again, I don't know anything about rays so please don't judge if it's an obvious mistake ;-;
using UnityEngine;
using System.Collections;
public class Raycast : MonoBehaviour {
Ray RayOrigin;
RaycastHit HitInfo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.E)){
RayOrigin = Camera.main.ViewportPointToRay(new Vector3(0,0,0));
if (Physics.Raycast(RayOrigin,out HitInfo,100f)){
Debug.DrawRay(RayOrigin.direction,HitInfo.point,Color.yellow);
}
}
}
}
At this moment it's creating a raycast on the edge of my camera's view and putting it to the same place on the map, no matter where I move the camera. Again, I don't know anything about rays so please don't judge if it's an obvious mistake ;-;