IndieGame/client/Assets/Scripts/Component/Common/MoveController.cs
2024-10-30 17:58:20 +08:00

52 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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