- Home /
RayCast relative to camera stays in 1 place.
When I start my game the raycast moves with the mouse movement but it stays in the same place where it begins.
using UnityEngine;
using System.Collections;
namespace CameraRayCastLine {
public class PickUpRayCast : MonoBehaviour {
// The camera gameobject.
public GameObject Camera;
// The camera position and direction.
// The relative camera position with axis.
float CameraPositionX;
float CameraPositionY;
float CameraPositionZ;
// The relative camera direction.
Vector3 CameraDirection;
// The camera position in Vector3 form.
Vector3 CameraPositionVector;
// The RayCast information.
// The length of the RayCast.
public float RayCastLength = 10;
// The color of the RayCast in the debug.
public Color CameraRayCastColor = Color.yellow;
// The bool for the RayCast.
public bool CameraRayCast;
void Start () {
// The code that finds the camera.
Camera = GameObject.Find("MainCamera");
// The code that gets the raw camera position axis and converts them to floats.
CameraPositionX = Camera.transform.position.x;
CameraPositionY = Camera.transform.position.y;
CameraPositionZ = Camera.transform.position.z;
// The code that changes the floats into a refined Vector3.
CameraPositionVector = new Vector3 (CameraPositionX, CameraPositionY, CameraPositionZ);
// The refining of the RayCast.
CameraRayCast = Physics.Raycast(CameraPositionVector, CameraDirection, RayCastLength);
}
void Update () {
// The camera direction refined into Vector3 form.
Vector3 CameraDirection = Camera.transform.TransformDirection(Vector3.forward);
// The actual RayCast.
CameraRayCast = true;
// The debug line that makes the raycast visible in the scene view.
Debug.DrawRay(CameraPositionVector, CameraDirection, CameraRayCastColor, RayCastLength);
}
}
}
You stated that with mouse$$anonymous$$ovement but i dont see any line that contains Input.mousePosition nor a line of similiar reflection.You should transform direction of Input.mousePosition not Vector3.forward.
@alph1 I have the mouse movement in a seperate script. But that's not the problem. The rotation works fine. The position is the problem.
Answer by AtGfx · Apr 21, 2017 at 04:36 PM
@RobertVanDerLeeuw
Your problem is that you do not update your CameraPositionVector.
In the Start method you do :
// The code that changes the floats into a refined Vector3.
CameraPositionVector = new Vector3 (CameraPositionX, CameraPositionY, CameraPositionZ);
and you do not update this position in the Update method (so it doesn't change, and keep the initial value).
More, I don't really understand why you declare
// The relative camera position with axis.
float CameraPositionX;
float CameraPositionY;
float CameraPositionZ;
// The relative camera direction.
Vector3 CameraDirection;
// The camera position in Vector3 form.
Vector3 CameraPositionVector;
since all the information is contained in the camera itself. I think you could delete these member variables and simply make :
Debug.DrawRay(Camera.transform.position, Camera.transform.forward, CameraRayCastColor, RayCastLength);
Answer by Potato_Blyat · Apr 21, 2017 at 04:36 PM
im in a hurry, but shortly put, your ray will go forwards in the z axis, from where the camera is at, now i asume you want to have it aim towards the mouse, in which case i would recommend you make a ray variable like so: Ray ray = Camera.main.ScreenToRayPoint(Input.mousePosition); (that's how it goes if memory serves right).
then the ray wil always go from the camera, to your mouse and then you can put in length and all that after.
sorry if this isn't the answer you were looking for but again, in a hurry, best of luck.
Your answer
Follow this Question
Related Questions
Why is my cursor not moving with the updating position of the raycast hit point? 0 Answers
camera acting weird... 1 Answer
Check if player object is right above this object 1 Answer
Raycast Object Selection 3 Answers
Unity 2D Position Issues 0 Answers