Hi
I am learning Unity3D so i am absolute beginner in this. I have plane (as ground), on plane i have cube with colider and rigidbody and then i have firstperson controller. I want to go with FPC (firstperson controller) to a cube and click on it with left button on mouse and i expect, that gives me a log in console. Script is attached to cube.
Thanks for help
My script code:
#pragma strict
function OnMouseDown() {
Debug.Log("Left Click");
}
Use a Raycast using the cursor position from camera to far away:
#pragma strict
var hit : RaycastHit;
//The range of the raycast
var raycastRange : float;
var actualCamera : Camera;
function Update () {
var ray : Ray = actualCamera.ScreenPointToRay (Input.mousePosition);
//Debug the ray in green
Debug.DrawRay(ray.origin, ray.direction * raycastRange, Color.green);
if(Physics.Raycast (ray, hit, raycastRange)) {
if(Input.GetMouseButtonDown(0)){
Debug.Log("Touched " + hit.collider.name);
//You can use tag for it
if(hit.collider.tag == "YourTagHere"){
Debug.Log("Touched " + hit.collider.name + " with the " + hit.collider.tag + " tag.");
}
}
}
}
Attach this script to camera.