-
Notifications
You must be signed in to change notification settings - Fork 0
/
SantaController.cs
39 lines (33 loc) · 1.21 KB
/
SantaController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
public class SantaController : MonoBehaviour
{
public float moveSpeed = 5f; // Movement speed of Santa
private Vector2 moveDirection; // Direction of movement
private Animator animator; // Reference to the Animator component
void Start()
{
// Get the Animator component attached to Santa
animator = GetComponent<Animator>();
}
void Update()
{
// Move Santa based on the current direction
transform.Translate(moveDirection * moveSpeed * Time.deltaTime);
// Update animation state
if (moveDirection != Vector2.zero)
{
animator.SetBool("isWalking", true); // Trigger walk animation
}
else
{
animator.SetBool("isWalking", false); // Trigger idle animation
}
}
// Called by UI buttons to set movement direction
public void MoveUp() => moveDirection = Vector2.up;
public void MoveDown() => moveDirection = Vector2.down;
public void MoveLeft() => moveDirection = Vector2.left;
public void MoveRight() => moveDirection = Vector2.right;
// Called when no button is pressed (stop moving)
public void StopMoving() => moveDirection = Vector2.zero;
}