Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Raddish · Dec 01, 2014 at 01:06 PM · animationbuttonsizeclickincrease

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HarshadK · Dec 01, 2014 at 11:49 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
             }
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges