How to trigger a BoxCollider with a RigidBody?
Firstly, let me mention that this is my first project in Unity and that I have no experience before yesterday.
I am trying to create something relatively simple, where there is a button that plays a sound when the mouse hovers over it. I have an object named input_mouse (it contains its own Collider and RigidBody) with the follwing script included:
#pragma strict
static var mousePos: Vector3;
var cam: GameObject;
cam = GameObject.Find("mainCamera");
var trigger: GameObject;
trigger = GameObject.Find("trigger_pranked");
function Update(){
mousePos = cam.GetComponent.<Camera>().ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y,1));
transform.position = mousePos;
}
This part is working just fine, except that the Z coordinate is interpreted by the program as Z-1. For whatever reason, Unity takes the number "1" from the code above and puts the coordinates of input_mouse as( X-coord,Y-coord, 0). That's off-putting, but tolerable.
Anyway, the real issue for me is getting the button to get triggered. On the button, I have the a script with following code:
#pragma strict
function OnTriggerEnter(other: Collider){
Debug.Log("Hello");
GetComponent.<AudioSource>().Play();
}
The debug "hello" is just a way for me to be sure that the button is being triggered, and as far as I'm aware it isn't, even though input_mouse enters into its collider zone. As far as I'm aware, this should work, but for whatever reason it isn't. Can anyone explain if I made any errors, or how to fix this?
mousePos = cam.GetComponent.().ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y,1));
the z in this case will be -9 because camera at -10 and if -10 considered as zero then -9 will considered 1 . highlight the object & hit play & look at the z transform number in the inspector. lastly check this documentation