在特定帧冻结动画。

huangapple go评论93阅读模式
英文:

Freeze animation at a specific frame

问题

我需要一些帮助。我在一个2D Unity游戏中工作,游戏的主要角色是一个毛绒球。这个毛绒球有一个手部组件(子对象),其中包含一个动画。每个独立的帧都具有盒子碰撞器属性。当我按下"E"键时,这只手将被激活,并且动画将按照我的光标方向启动。所以,问题是:我希望这只手能够抓住东西,比如一个箱子。在这里,我请求您的帮助。我试图弄清楚如何在特定帧的盒子碰撞器与对象(如地面或箱子)碰撞时,将动画冻结一段时间,比如5秒,或者再次按下"E"键。这是手的脚本:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class facingScript : MonoBehaviour
  5. {
  6. private SpriteRenderer render;
  7. private Camera mainCam;
  8. private Vector3 mousePos;
  9. private Animator aniHand;
  10. public bool freezeAnimation = false;
  11. void Start()
  12. {
  13. render = GetComponent<SpriteRenderer>();
  14. mainCam = Camera.main;
  15. gameObject.SetActive(false);
  16. aniHand = GetComponent<Animator>();
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. mousePos = Input.mousePosition;
  22. mousePos = mainCam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, mainCam.transform.position.z - transform.position.z));
  23. Vector3 rotation = mousePos - transform.position;
  24. if (rotation.x > 0)
  25. render.flipY = true;
  26. else
  27. render.flipY = false;
  28. if (Input.GetKeyDown(KeyCode.E))
  29. {
  30. aniHand.Play("CharacterHand");
  31. }
  32. }
  33. void OnCollisionEnter2D(Collision2D collision)
  34. {
  35. if (collision.gameObject.tag == "box")
  36. {
  37. StartCoroutine(HandCollide());
  38. }
  39. }
  40. private IEnumerator HandCollide()
  41. {
  42. if (!freezeAnimation)
  43. {
  44. aniHand.speed = 0; // 冻结动画
  45. yield return new WaitForSeconds(5); // 等待5秒
  46. aniHand.speed = 1; // 恢复动画速度
  47. freezeAnimation = true; // 防止再次冻结
  48. }
  49. }
  50. }

你可以尝试上述脚本中的更改,它会在盒子碰撞时冻结手的动画,并在5秒后恢复动画速度。

英文:

I need some help . I work at a 2d unity game which have a fluffy ball as the main character . This fluffy ball have a hand component ( child ) which have a animation . Each individual frame have a box collider property .This hand will be activated when i press E and that animation will start in the direction where my cursor is . So , the problem is next : I want that hand to grab stuffs . Like a box . Here i ask for your help . i tried to figure out how i can freeze the aniamation for a short time , like 5 seconds or till i press again E , when the box collider of that specific frame collide with a object (e.g. ground or box ).Here is the script of the hand :

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class facingScript : MonoBehaviour
  5. {
  6. private SpriteRenderer render;
  7. private Camera mainCam;
  8. private Vector3 mousePos;
  9. private Animator aniHand;
  10. public bool freezeAniamtion = false;
  11. void Start()
  12. {
  13. render = GetComponent&lt;SpriteRenderer&gt;();
  14. mainCam = Camera.main;
  15. gameObject.SetActive(false);
  16. aniHand = GetComponent&lt;Animator&gt;();
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. mousePos = Input.mousePosition;
  22. mousePos = mainCam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, mainCam.transform.position.z - transform.position.z));
  23. Vector3 rotation = mousePos - transform.position;
  24. if(rotation.x &gt; 0)
  25. render.flipY = true;
  26. else
  27. render.flipY = false;
  28. if(Input.GetKeyDown(KeyCode.E))
  29. {
  30. aniHand.Play(&quot;CharacterHand&quot;);
  31. }
  32. }
  33. void OnCollisionEnter2D(Collision2D collision)
  34. {
  35. if(collision.gameObject.tag == &quot;box&quot;)
  36. {
  37. StartCoroutine(HandCollide());
  38. // help pls :)
  39. }
  40. }
  41. private IEnumerator HandCollide(float waitTime)
  42. {
  43. // idk what i should put here :P
  44. }
  45. }`

I try with aniHand.speed but it dont do nothing and with PlayInFixedTime but idk how it work

答案1

得分: 0

So . 我以某种方式找到了解决办法 . 我有另一个脚本,可以在1秒内激活和停用我的手。当我尝试停止手部脚本中的动画时,它只是从另一个脚本中关闭。

英文:

So . I fugire it out some how . I have another script that activate and dezactivate my hand for 1 second. And when I try to stop my animation în hand script it just turn of from that another script

huangapple
  • 本文由 发表于 2023年2月6日 20:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定