UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionHaving a Raycast come out from the center of the camera EmptyHaving a Raycast come out from the center of the camera

more_horiz
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 ;-;

descriptionHaving a Raycast come out from the center of the camera EmptyRe: Having a Raycast come out from the center of the camera

more_horiz
new Vector3(0,0,0) try using Camera.main.position or mouse.position code looks fine.
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply