IndieGame/client/Assets/Scripts/Component/Common/MoveController.cs

52 lines
1.2 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00
using NodeCanvas.Framework;
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour
{
Rigidbody2D rigidbody2d;
Blackboard blackboard;
2024-10-30 17:58:20 +08:00
[LabelText("速度属性名称")]
2024-10-11 10:12:15 +08:00
public string speedName = "speed";
2024-10-30 17:58:20 +08:00
[LabelText("方向属性名称")]
2024-10-11 10:12:15 +08:00
public string directionName = "moveDirection";
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
if (rigidbody2d == null)
{
2024-10-30 17:58:20 +08:00
Debug.LogError("Rigidbody2D组件为空");
2024-10-11 10:12:15 +08:00
return;
}
blackboard = GetComponent<Blackboard>();
if (blackboard == null)
{
2024-10-30 17:58:20 +08:00
Debug.LogError("Blackboard组件为空");
2024-10-11 10:12:15 +08:00
return;
}
}
private void Update()
{
if(rigidbody2d && blackboard && blackboard.GetVariableValue<Vector2>(directionName) != Vector2.zero)
{
MovePosition();
}
}
private void MovePosition()
{
float speed = blackboard.GetVariableValue<float>(speedName);
2024-10-30 17:58:20 +08:00
Vector2 tempMove = blackboard.GetVariableValue<Vector2>(directionName) * (0.02f * speed);
2024-10-11 10:12:15 +08:00
rigidbody2d.MovePosition(rigidbody2d.position + tempMove);
}
}