- Home /
The question is answered, right answer was accepted
i don't understand Vector3.forward correctly on camera
Hi, I am trying to get a raycast from the camera to forward, but it's not working correclty, it's pointing forward in the world space and not in camera local position. I really don't know what to say more about this problem, but I'll leave you the scripts for the camera and the one I'm trying to raycast from. Oh, and I'm not using a regular character controller, I'm using a rigidbody and also using a different script for the camera. this is the script where I'm trying to raycast:
#pragma strict
var range : float;
var bombs : int = 1;
var bomb : Transform;
function Update ()
{
Debug.DrawRay(transform.position, Vector3.forward * range, Color.red);
if (Input.GetMouseButtonDown(0) && bombs >=1)
{
var Hit : RaycastHit;
if (Physics.Raycast(transform.position, Vector3.forward , Hit, range))
{
if(Hit.collider.gameObject.tag == "Ground")
{
Debug.Log ("Found Ground");
var bomb : Transform = Instantiate (bomb, Hit.collider.gameObject.transform.position, Quaternion.identity);
}
}
}
}
And here is the camera script:
#pragma strict
var lookSensivity : float = 5;
@HideInInspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var lookSmoothDamp : float = 0.1;
function Start () {
Screen.lockCursor = true;
}
function Update ()
{
yRotation += Input.GetAxis("Mouse X") * lookSensivity;
xRotation -= Input.GetAxis("Mouse Y") * lookSensivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
if (Input.GetButtonDown("Esc"))
Screen.lockCursor = false;
if (Input.GetButtonDown("Fire1"))
Screen.lockCursor = true;
}
I don't know what you mean by this phrase: "raycast from the camera to forward". Typically you would want to raycast from the forward of the object using 'transform.forward.'
Debug.DrawRay(transform.position, transform.forward * range, Color.red);
If for some reason you want the camera forward, you can use 'Camera.main.transform.forward'.
Sorry for my english, I want to raycast from the forward of the object, can you try using the scripts in a roject? add a camera, put the camera script on it, then put the bombplacer script on it, the raycast is not pointing correctly
Your code above use 'Vector3.forward'. You want 'transform.forward' if you want the forward of the object.
Answer by Vonni · May 10, 2013 at 02:04 PM
Vector3.forward is the same as Vector3(0,0,1), its just an abreviation. BUT, when you use transform.forward, it takes the direction of the local Z-axis on the object. More importantly, try to print values when you can, or when things aren't working as expected. Like: print(Vector3.forward)
. Always use these tests to find out where things are going wrong.
Follow this Question
Related Questions
cast a ray to hit an object in the center of the screen 1 Answer
How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers
Camera Zoom in&out 2 Answers
Natural camera sway 1 Answer
Ray through camera 1 Answer