【发布时间】:2021-12-22 20:38:50
【问题描述】:
如果我一开始的英语不好,我很抱歉。
The button 这是我想要在按下时运行动画的按钮。
Sprites我想在按下按钮时在这两种状态之间切换
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class Button : MonoBehaviour
{
private Animator animator;
public void SetTrigger(string Pressed) { }
Collider2D col;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
animator.SetFloat("Position", 0);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if (col == touchedCollider)
{
animator.SetFloat("Position", 1);
}
}
}
}
}
这是我上次尝试后代码的样子,它可能没有意义,因为我在测试时一直在混合。我已经尝试过使用浮点数、布尔值和触发器。使用布尔它对我来说已经工作了一半,如果我触摸按钮它没有下沉但是如果从统一我按下按钮手动将布尔值更改为true,并且在从移动屏幕触摸后它识别出触摸并将按钮返回到原来的位置。在所有情况下,触摸控制都不好用,但我已经修改了触摸控制代码,我会说它很好。
对不起,如果没有很好理解,我是统一和编程的新手,而且英语不是我的主要语言
【问题讨论】: