Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to play animation by clicking on the model with mouse or finger in Unity?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Unity how to achieve mouse or finger click on the model to play animation, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Using UnityEngine;using System.Collections; public class ClickPlayAnimation: MonoBehaviour {/ implementation function is to click on the model to play animation / use method, add collision to the model, add script / bool isPlayAnim = false; / / Use this for initialization void Start () {} / / Update is called once per frame void Update () {/ / animation.Play () If (Application.platform = = RuntimePlatform.IPhonePlayer | | Application.platform = = RuntimePlatform.Android) {foreach (Touch touch in Input.touches) {if (Input.touchCount > 0 & & Input.GetTouch (0). Phase = = TouchPhase.Began | | Input.GetTouch (0). Phase = = TouchPhase.Moved) {Ray ray = Camera.main.ScreenPointToRay (new Vector3 (touch.position.x, touch.position.y, 0); RaycastHit hit; if (Physics.Raycast (ray, out hit)) {/ / Debug.DrawLine (ray.origin, hit.point) If (hit.collider.gameObject.name = = gameObject.name) {isPlayAnim = true; print;} else {if (Input.GetMouseButtonDown (0)) {Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); RaycastHit hit; if (Physics.Raycast (ray, out hit)) {Debug.DrawLine (ray.origin, hit.point); / / print (hit.collider.gameObject.name); / / curObject = hit.collider.gameObject If (hit.collider.gameObject.name = = gameObject.name) {isPlayAnim = true; print ("123");} / / display the name of the currently selected object / / print (hit.collider.gameObject);} if (isPlayAnim) {animation.Play (); isPlayAnim = false;}

This is the answer to the question about how to realize the mouse or click on the model to play the animation in Unity. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report