在特定帧冻结动画。

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

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&lt;SpriteRenderer&gt;();
        mainCam = Camera.main;
        gameObject.SetActive(false);
        aniHand = GetComponent&lt;Animator&gt;();
    }

    // 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 &gt; 0)
            render.flipY = true;
        else
            render.flipY = false;

    
            if(Input.GetKeyDown(KeyCode.E)) 
            {
            aniHand.Play(&quot;CharacterHand&quot;);
            }
            

    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == &quot;box&quot;)
        {
            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

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:

确定