Ray Functions with GUI

Well, I’m using DragRigidbody with some restrictions on distance, though that isn’t my problem.

I’ve been trying to render and delete a texture depending on the mouse state.

I was trying to get it to work with PlayMaker, but I’ve been getting some loops, and I can’t seem to fix it, so I’m asking here.

How would I go about doing something like this?:


MouseState_Over: RenderTexture_Hand

MouseState_Click: RenderTexture_HandGrabbing : DeleteTexture_Hand

MouseState_Unclick: DeleteTexture_Handgrabbing : RenderTexture_Hand

MouseState_UnOver: DeleteTexture_Hand


Something sort of like in the HPL Engine (Amnesia), where when it’s mouseover, and it shows a hand. You click, it picks up the object (DragRigidBody with Range Addon), and shows a hand grabbing.

I’m looking for the hand rendering script, since DragRigidBody is sufficient enough for me.

Thanks.

2 Answers

2

I’m assuming you use javascript:

var mpos = Vector2.zero; //assign mouse position to this

var mstate = 0; //could use an [**enum**][1] for this

var mdown = false;

var texture_HandOver : Texture2D;//Assign your normal "hover/over" cursor texture to this

var texture_HandGrab : Texture2D;//Assign your grab cursor texture to this

var hoveringObject = false;//It's up to you to make the logic that triggers this.

function Update()
{

    if(Input.GetMouseButtonDown(0))//check if right mbtn was clicked this frame
    {

        mstate = 1;//1 = handgrabbing
        mdown = true;

    }

    if(Input.GetMouseButtonUp(0))//check if right mbtn was clicked this frame
    {

        if(mdown)
        {
            /*
            Triggers if the mouse was held down in a previous frame then released in this    
            frame.
            */
            mstate = 0;//back to default
            mdown = false;

        }

    }

}

function OnGUI()
{

    switch(mstate)
    {

    case 0:
        if(hoveringObject)
        {
            GUI.DrawTexture(
                new Rect(mpos.x, mpos.y, texture_HandOver.width, texture_HandOver.height),  
                texture_HandOver
                );
        }

        //if no object is hovered then the hand simply wont be drawn.

        break;
    case 1:
        GUI.DrawTexture(
                new Rect(mpos.x, mpos.y, texture_HandGrab.width, texture_HandGrab.height),  
                texture_HandGrab
                );
        break;
    }

}

ref links:

http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html
http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonUp.html

Thanks! This is perfect for what I need.

I’m new to scripting, but I’m sure I can figure it out.

Thanks again!

Glad I could help.