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