Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Having a Raycast come out from the center of the camera

Having a Raycast come out from the center of the camera

Go down  Message [Page 1 of 1]


Member [Lv. 1]
Member [Lv. 1]
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 () {

RayOrigin = Camera.main.ViewportPointToRay(new Vector3(0,0,0));
if (Physics.Raycast(RayOrigin,out HitInfo,100f)){


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

View user profile


Member [Lv. 3]
Member [Lv. 3]
new Vector3(0,0,0) try using Camera.main.position or mouse.position code looks fine.

View user profile http://jakeaquilina.weebly.com/

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum