Right Mouse Down?

How can I check if I have right clicked on a collider? I know how to use:

function OnMouseDown () {}

But is there one for right clicking??

The solution provided by @clunk47 will work, but it is casting a ray every frame and I’m not sure if that is what you intend. If you only want the ray to be cast when the right-click happens you can do this:

JavaScript

#pragma strict

function Update()
{
	if(Input.GetMouseButtonDown(1))
	{
		OnRightClick();
	}
}

function OnRightClick()
{
	// Cast a ray from the mouse
	// cursors position
	var clickPoint : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	var hitPoint : RaycastHit;

	// See if the ray collided with an object
	if (Physics.Raycast(clickPoint, hitPoint))
	{
		// Make sure this object was the
		// one that received the right-click
		if (hitPoint.collider == this.collider)
		{
			// Add code for the right click event
			Debug.Log("Right Clicked on " + this.name);
		}
	}
}

C#:

using UnityEngine;
using System.Collections;

public class RightClick : MonoBehaviour 
{
	void Update () 
	{
		if(Input.GetMouseButtonDown (1))
			OnRightClick();
	}
	
	// Check for Right-Click
	void OnRightClick()
	{
		// Cast a ray from the mouse
		// cursors position
		Ray clickPoint = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hitPoint;

		// See if the ray collided with an object
		if (Physics.Raycast(clickPoint, out hitPoint))
		{
			// Make sure this object was the
			// one that received the right-click
			if (hitPoint.collider == this.collider)
			{
				// Put code for the right click event
				Debug.Log("Right Clicked on " + this.name);
			}
		}
	}
}

Just use a RaycastHit and a Ray, and Input.MouseButtonDown(1), the ‘1’ being mouse button 1, aka right button. 0 is left click, 2 is middle button. You can attach this to an empty GameObject, but I usually just attach to the main camera, just makes sense to me.

#pragma strict

var ray : Ray;
var hit : RaycastHit;

function Update () 
{
	ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	if(Physics.Raycast(ray, hit))
	{
		if(Input.GetMouseButtonDown(1))
		{
			Debug.Log(hit.collider.gameObject.name);
		}
	}
}

Since OnMouseDown works only for left button, I use following approach to mimic right button event:

private bool mouseOver;

void Update()
{
    if (mouseOver && Input.GetMouseButtonDown(1))
    {
        OnRightMouseDown();
    }
}

private void OnMouseEnter()
{
    mouseOver = true;
}

private void OnMouseExit()
{
    mouseOver = false;
}

private void OnRightMouseDown()
{
    Debug.Log(transform);
}

It’s more or less performance optimal solution I think, as you don’t have to cast a ray on every frame and/or every object, in case if you have many of them.