Hi good people,
I am new to unity and javascript.
This question is probably addressed in the past, but I can’t find one.
I’m trying a simple interactive here.
I have a Cube (with collider naturally).
I want to click the box with mouse button and make it rotate.
However I can’t seem to get it done correctly.
I used the code:
function Update () {
if (Input.GetKey (“mouse 0”)) {
print (“Box CLICKED!”);
}
}
but it works even if I click NOT on the cube.
I tried with function OnMouseUp but it doesn’t seem to work.
Advice pls :?
Thanks in advance :)[/code]
Use OnMouseDown instead of Update
Your script is a MonoBehaviour, which gives you many more event callbacks than just update
WOW! a reply in 3 mins!!
Thanx for the pointer.
Yes I have looked through the MonoBehaviour and changed my event from update to OnMouseDown.
Therefore the script:
function OnMouseDown () {
if (Input.GetKey ("mouse 0")) {
print ("Box Clicked!");
}
}
It doesn’t seem to work, the print doesn’t come out.
Any idea what did I do wrong?
Or can give me example?
strange:
Are you sure “mouse 0” is set in the input manager ?
I Hope you’ve put the script in the cube gameobject ?
you can also try to use Input.GetMouseButton(0)
Wah thanks for the reply so far…
Hmmm… So logically the script should work?? :shock:
Yes I have put the mouse 0 under Fire1 set in the input manager.
Yes of course I have applied the script onto the Cube.
Strangely the script works fine with update, but it doesn’t detect collision on the Cube. I want to activate the script only when I click the Cube.
It is a simple scene with one Cube, one Camera and (supposibly) three-lined script.
What could go wrong :shock:
Anymore idea?
:?
not sure about “print”
try Debug.Log instead
Don’t use GetKey.
function OnMouseDown () {
print ("Box Clicked!");
}
–Eric
1 Like
Well it doesn’t work with transform.Translate as well.
Basically any script under function OnMouseDown doesn’t seem to be activated.
Thanks for the reply.
I will continue tomorow
Tried that… not working as well
The only ways I know of that wouldn’t work are 1) no collider on the box, 2) something in front of the box that’s not on the IgnoreRaycast layer, or 3) the box itself is on the IgnoreRaycast layer.
–Eric
From the code that you posted, it doesn’t seem like there’s any Raycasting going on to determine what’s actually -been- clicked…
// Did we hit the surface?
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
if (Physics.Raycast (ray, hit))
{
// Do whatever you want to detect what's been hit from the data stored in the "hit" variable - this should rotate it...
var transform : Transform = hit.collider.GetComponent(Transform);
if (transform )
{
transform.localEulerAngles.x += 0.5;
}
}
A good place to start is the “DragRigidBody” script located in the Standard Assets script folder. Also the Mesh Deformation example in the Procedural Examples pack.
Note that you can also set Layer Masks to ignore certain layers when casting these rays - very useful!
You don’t have to do any of that if the object has a rigidbody of some type and a collider. If you have a collider with matching rigidbody, function OnMouseDown is called every time the mouse clicks on the object.
If you don’t want to do it that way, then you’re stuck doing calculations based on the object’s Camera.WorldToScreenPosition() and the position of the mouse at mouse-click.
Note: Be sure to set the rigidbody.useGravity to false, isKinematic to true, and freezeRotation to true if you don’t want the cube moving just yet.
You don’t need a rigidbody, just a collider. OnMouseDown() uses raycasting; you don’t have to write it out yourself. The script I posted is 100% functional as-is. Oh yes, one other way it won’t work: 4) You didn’t attach the script to the box…
–Eric
1 Like
Wow - I never knew! Thanks guys!
I guess it’s since my use was for basically any object in the world, but this is much more convenient.
People, try to keep it simple, kinda lost in here
- Yes, there is a box collider (created by default when a created a cube game object)
- There is only 2 objects in the scene: a cube and a camera. Unless the camera is the one blocking the raycast, there is no other object.
- I don’t really get the IgnoreRaycast layer thingy, but there is no layer created.
- Yes of course I have attached the script onto the cube.
Still not working
Just for info: I’m using the latest release 2.5 for Windows, trial version.
Is it more helpful if I post the scene file here?? :?
Thanks!!
Yep, because with that setup it should really just work.
–Eric
Here’s the scene file and my js document I extracted from Assets folder.
Please have a look and thanks for the help
133521–4933–$assets_199.zip (3.21 KB)
OH!! FOUND THE PROBLEM!!
I have been using the project from other version (2.5 beta).
I don’t think the compiler works the same way in the final release.
I created a new project and copy the Scene and the Script over and it works like magic!!
Thanks for all the trouble, ignore the previous post.
This is embarassing :roll:
Many, many thanks again
HI Jeff,
My code and project is almost identical to yours. I’m trying to draw my ray in a similar way. When your ray is drawn OnMouseDown, does it stay on screen when OnMouseUp? What if you where to move your camera, does the ray dissapear? I ask because I checked for the hit and it is happening. But the following Debug.DrawLine or Debug.DrawRay does not show on screen.
I was wondering if this might be because the code is within the OnMouseDown function and not continuously run in the update() function.
Yes…the line is only drawn for one frame, so if you want to see it, you have to add whatever logic is necessary to make it be drawn longer.
–Eric