- Home /
I need advice on how to make a right click system similar to runescapes?
I am not asking for any code.
What I'm looking for is advice on how to do a right clicking system like runescapes. The only ways I can think of how to do this, is make a script for every clickable object in the game, but that would be a HUGE waste of space. Obviously this is not how I should do it.
The thing is, when you right click nothing / the ground, it should say nothing and only with a close button, but when you right click lets say a store keeper, there should be options like Talk, Store, Quest, Close. How can I make a system that detects what options the clicked object has, then puts it into an array? If any expert at Unity code could explain how this could be achieved, I would be incredibly grateful. Such a small thing is just being a huge problem for me to solve without wasting space. Thank you!
Answer by Loius · Jul 11, 2014 at 09:51 PM
public class Clickable : MonoBehaviour {
public virtual string[] Options { get { return new string[0]; } }
public virtual void HandleClick() {
}
}
public class Shopkeeper : Clickable {
public override string[] Options { get { return base.options.Concatenate(new string[]{"Sell","Buy"}; } }
public override void HandleClick() {
DisplayShopMenu();
}
}
...
public class PlayerInput : MonoBehaviour {
void Update() {
if ( Input.GetKeyDown(KeyCode.Mouse1) ) {
RaycastHit rch = new RaycastHit();
if ( Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition, out rch) ) { // this line is wrong, needs more arguments
or something
Clickable c = rch.collider.GetComponent<Clickable>();
if ( c ) c.HandleClick();
else HandleUnclickable();
}
}
}
}
Keyword: Inheritance.
":" becomes "extends", "void Name()" becomes "function Name()", "Type Name()" becomes "function Name() : Type", "Type variable" becomes "var variable : Type", "base" becomes "super"
i don't know if get/set works in unityscript. they're called 'properties', which will help searching for a fix if they don't. they can also just be rewritten as functions; that's basically all they are.
also search for 'convert cs to unityscript/javascript' because there are more questions about that than there are humans