52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
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);
|
||
}
|
||
|
||
}
|