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;
[LabelText("<22>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public string speedName = "speed";
[LabelText("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public string directionName = "moveDirection";
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
if (rigidbody2d == null)
{
Debug.LogError("Rigidbody2D<32><44><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD>");
return;
}
blackboard = GetComponent<Blackboard>();
if (blackboard == null)
{
Debug.LogError("Blackboard<72><64><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD>");
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);
}
}