_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/SnailHouse/Snailmove.cs

72 lines
1.7 KiB
C#
Raw Normal View History

2024-12-08 20:16:04 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-12-09 15:33:32 +08:00
using UnityEngine.UI;
2024-12-08 20:16:04 +08:00
public class Snailmove : MonoBehaviour
{
private float moveTimer = 0f; // <20><>ʱ<EFBFBD><CAB1>
private float moveSpeed = 10f; // <20>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
public Vector3 moveDirection; // <20><>ǰ<EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
public Animator animator; // Animator<6F><72><EFBFBD><EFBFBD>
private bool isMoving = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ƶ<EFBFBD>
public long id;
2024-12-09 15:33:32 +08:00
public Image Image;
2024-12-09 14:33:01 +08:00
private void Start()
2024-12-08 20:16:04 +08:00
{
moveDirection=Vector3.right;
}
private void Update()
{
// ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
moveTimer += Time.deltaTime;
// ÿ3<C3BF><33><EFBFBD>ı<EFBFBD>һ<EFBFBD>η<EFBFBD><CEB7><EFBFBD>
if (moveTimer >= 10f)
{
ChangeDirection();
moveTimer = 0f;
}
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
transform.Translate(moveDirection * moveSpeed * Time.deltaTime);
if (transform.localPosition.x <= -600 || transform.localPosition.x >= 600)
{
transform.localPosition = new Vector3(0, transform.localPosition.y, transform.localPosition.z);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>
private void ChangeDirection()
{
int randomDirection = Random.Range(0, 3);
if (randomDirection <= 1)
{
moveDirection = Vector3.right;
2024-12-09 15:33:32 +08:00
Image.transform.rotation = Quaternion.Euler(0, 0, 0);
2024-12-08 20:16:04 +08:00
animator.SetBool("move",true);
}
if(randomDirection <= 2&& randomDirection > 1)
{
2024-12-09 15:33:32 +08:00
moveDirection = Vector3.left;
Image.transform.rotation = Quaternion.Euler(0, 180, 0);
2024-12-08 20:16:04 +08:00
animator.SetBool("move", true);
}
else
{
moveDirection = Vector3.zero;
animator.SetBool("move", false);
}
}
}