Interacting with game objects by mouse click.
So I watched a few videos of people playing games with the oculus. The games they were playing only used the mouse(click/scroll). For example (https://www.youtube.com/watch?v=yNZHVSV_CyU), that game is only using the mouse and you can interact with different objects to trigger animations. I believe it has something to do with raycasting, although I haven't mastered that yet. Does anyone have an idea how I could "click" on a gameobject, raycast, hit, then have the hit object to run animation? It seems very daunting.
Like I said I'm new to raycasting and its parameters.
Something similar to this:
if (Physics.Raycast(camera.position, out hit)) {
hit.gameobject.animation.play(""):
}
this is a example code, not tested, to do something like you are looking. I think to its enough commented.
// maybe in a function called from update
Ray ray;
RaycastHit hit;
//take the vector from camera to mouse
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//to cast something you need to have a collider component in gameobject
if (Physics.Raycast (ray, out hit)) {
//if left click
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
//get the animation
Animation anim = hit.collider.gameObject.GetComponent<Animation> ();
//if animation exists
if (anim != null) {
//anim.play() take the default
//to specific animation replace for anim.play("specific animation")
anim.Play ();
}
}
}
Your answer
Follow this Question
Related Questions
Control UI Input Field entirely with code? 1 Answer
GetAxis help? 1 Answer
input.getaxis problem 0 Answers
Getting mouse input in screen (0,0) and drag mouse 0 Answers