Hello folks.
I have some trouble with this Raycast script. I admit, I’m not that good at coding yet, so I thought I would seek some help here.
This is what I want the script to do:
1: Detect if the player is currently looking at a bottle (Could be any game object)
2: Print that you’re looking at the object if you are.
3: If you are looking at an object, and press the mouse button 0, Destroy the object and print a message
Now, this is what I have so far:
var distance = 10;
function Update ()
{
Screen.showCursor = false;
var dir = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, dir * distance, Color.blue);
if( Physics.Raycast( transform.position, dir, hit, distance ) ){
if( hit.collider.gameObject.tag == "Quest Bottle" ){
print("You are looking at a bottle!");
if(Input.GetKey(KeyCode.Mouse0)){
Destroy(hit.collider.name);
print("Picked up a Bottle!");
}
}
}
}
However, this doesn’t work. I guess one of the problems could be that I cannot see the ray in-game, therefore, I don’t know where to “look” to pick up the object. Secondly, I’m not sure this is actually how it should be written.
Anybody has some inputs?