- Home /
mouse clicked on object animation c#
Hi, im trying to make a small part of my game where when the player click on the game object, the player will know that they have clicked it.
E.g When I left mouse button click on the object, the object size increases then returns back to its original size. Allowing the player to know that they have clicked that object.
You need Raycast and Coroutine ($$anonymous$$anual and Scripting API).
Also there are similar questions relating to changing size or material/texture on selecting an object on UA and Internet.
Answer by jenci1990 · Dec 01, 2014 at 05:45 PM
Create a new "Mouse" C# script and paste it:
using UnityEngine;
using System.Collections;
class Mouse : MonoBehaviour {
public static bool isLeftButton = false;
public static bool isRightButton = false;
public static bool isMiddleButton = false;
public static bool isLeftButtonDown = false;
public static bool isRightButtonDown = false;
public static bool isMiddleButtonDown = false;
public static GameObject onLeftButtonDown;
public static GameObject onRightButtonDown;
public static GameObject onMiddleButtonDown;
public static GameObject onLeftButtonUp;
public static GameObject onRightButtonUp;
public static GameObject onMiddleButtonUp;
public static GameObject onLeftButton;
public static GameObject onRightButton;
public static GameObject onMiddleButton;
public static GameObject onEnter;
public static GameObject onOver;
public static GameObject onExit;
public static bool isMove = false;
public static Vector3 incrementPositionInWorld;
public static Vector3 absolutePositionInWorld;
private Ray mouseRay;
private RaycastHit hit;
void Update() {
if (Camera.main) {
isLeftButton = Input.GetMouseButton(0);
isRightButton = Input.GetMouseButton(1);
isMiddleButton = Input.GetMouseButton(2);
isLeftButtonDown = Input.GetMouseButtonDown(0);
isRightButtonDown = Input.GetMouseButtonDown(1);
isMiddleButtonDown = Input.GetMouseButtonDown(2);
mouseRay = Camera.main.ScreenPointToRay( new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f) );
Physics.Raycast(mouseRay,out hit);
Vector3 mousePos = Input.mousePosition;
if (hit.collider) {
mousePos.z = hit.collider.gameObject.transform.position.z - Camera.main.transform.position.z;
}else mousePos.z = -0.05f;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
incrementPositionInWorld = mousePos - absolutePositionInWorld;
if ( incrementPositionInWorld.magnitude > 0f ) isMove = true;
else isMove = false;
absolutePositionInWorld = mousePos;
onExit = null;
if ( (hit.collider && onOver != hit.collider.gameObject) || !hit.collider ) onExit = onOver;
if ( hit.collider ) {
if (onOver != hit.collider.gameObject) onEnter = hit.collider.gameObject;
else onEnter = null;
onOver = hit.collider.gameObject;
}else {
onEnter = null;
onOver = null;
}
if ( Input.GetMouseButton(0) ) onLeftButton = onOver;
else onLeftButton = null;
if ( Input.GetMouseButton(1) ) onRightButton = onOver;
else onRightButton = null;
if ( Input.GetMouseButton(2) ) onMiddleButton = onOver;
else onMiddleButton = null;
if ( Input.GetMouseButtonDown(0) ) onLeftButtonDown = onOver;
else onLeftButtonDown = null;
if ( Input.GetMouseButtonDown(1) ) onRightButtonDown = onOver;
else onRightButtonDown = null;
if ( Input.GetMouseButtonDown(2) ) onMiddleButtonDown = onOver;
else onMiddleButtonDown = null;
if ( Input.GetMouseButtonUp(0) ) onLeftButtonUp = onOver;
else onLeftButtonUp = null;
if ( Input.GetMouseButtonUp(1) ) onRightButtonUp = onOver;
else onRightButtonUp = null;
if ( Input.GetMouseButtonUp(2) ) onMiddleButtonUp = onOver;
else onMiddleButtonUp = null;
}
}
}
Attach this script to any gameObject in scene! Now you can access it from any other C# script like this:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
GameObject currentSelected;
private Vector3 originScale;
void Update () {
if (Mouse.isLeftButtonDown) {
print ("ok");
if (Mouse.onLeftButtonDown!=null && Mouse.onLeftButtonDown.tag == "Selectable") {
if (currentSelected != null) currentSelected.transform.localScale = originScale;
currentSelected = Mouse.onLeftButtonDown;
originScale = currentSelected.transform.localScale;
currentSelected.transform.localScale *= 1.5f;
Debug.Log("You Select: " + currentSelected);
}else {
if (currentSelected != null) currentSelected.transform.localScale = originScale;
currentSelected = null;
}
}
}
}
Your answer
Follow this Question
Related Questions
How To Play Animation On UI Button Click Please... Im Going To Freak Out Please Enter And Help Me 2 Answers
How can I stop an animator playing 0 Answers
Playing different button animation after first animation plays 1 Answer
Unity 4.6 UI Button Click Animation? 4 Answers
Two buttons behaving entirely differently (despite similar logic in code)? 1 Answer