英文:
Freeze animation at a specific frame
问题
我需要一些帮助。我在一个2D Unity游戏中工作,游戏的主要角色是一个毛绒球。这个毛绒球有一个手部组件(子对象),其中包含一个动画。每个独立的帧都具有盒子碰撞器属性。当我按下"E"键时,这只手将被激活,并且动画将按照我的光标方向启动。所以,问题是:我希望这只手能够抓住东西,比如一个箱子。在这里,我请求您的帮助。我试图弄清楚如何在特定帧的盒子碰撞器与对象(如地面或箱子)碰撞时,将动画冻结一段时间,比如5秒,或者再次按下"E"键。这是手的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class facingScript : MonoBehaviour
{
private SpriteRenderer render;
private Camera mainCam;
private Vector3 mousePos;
private Animator aniHand;
public bool freezeAnimation = false;
void Start()
{
render = GetComponent<SpriteRenderer>();
mainCam = Camera.main;
gameObject.SetActive(false);
aniHand = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
mousePos = Input.mousePosition;
mousePos = mainCam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, mainCam.transform.position.z - transform.position.z));
Vector3 rotation = mousePos - transform.position;
if (rotation.x > 0)
render.flipY = true;
else
render.flipY = false;
if (Input.GetKeyDown(KeyCode.E))
{
aniHand.Play("CharacterHand");
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "box")
{
StartCoroutine(HandCollide());
}
}
private IEnumerator HandCollide()
{
if (!freezeAnimation)
{
aniHand.speed = 0; // 冻结动画
yield return new WaitForSeconds(5); // 等待5秒
aniHand.speed = 1; // 恢复动画速度
freezeAnimation = true; // 防止再次冻结
}
}
}
你可以尝试上述脚本中的更改,它会在盒子碰撞时冻结手的动画,并在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 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class facingScript : MonoBehaviour
{
private SpriteRenderer render;
private Camera mainCam;
private Vector3 mousePos;
private Animator aniHand;
public bool freezeAniamtion = false;
void Start()
{
render = GetComponent<SpriteRenderer>();
mainCam = Camera.main;
gameObject.SetActive(false);
aniHand = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
mousePos = Input.mousePosition;
mousePos = mainCam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, mainCam.transform.position.z - transform.position.z));
Vector3 rotation = mousePos - transform.position;
if(rotation.x > 0)
render.flipY = true;
else
render.flipY = false;
if(Input.GetKeyDown(KeyCode.E))
{
aniHand.Play("CharacterHand");
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "box")
{
StartCoroutine(HandCollide());
// help pls :)
}
}
private IEnumerator HandCollide(float waitTime)
{
// idk what i should put here :P
}
}`
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论