Beginner Scripting: OnMouseDown

 

In this weeks Coding Task it looked at the OnMouseDown function. OnMouseDown can detect a click on a collider or a GUI text element. You must have a box collider and a rigidbody attached. You can use a DeBug Log to prove the OnMouseDown is working correctly. You can also use this code for many things like applying a force script when clicking.

The Script is as follows


using UnityEngine; using System.Collections; public class MouseClick : MonoBehaviour { private Rigidbody rb; private void Awake() { rb = GetComponent<Rigidbody>(); } void OnMouseDown () { rb.AddForce(-transform.forward * 500f); rb.useGravity = true; } }


Comments

Popular Posts