您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

81 行
3.0 KiB

using Unity.VisualScripting;
using UnityEngine;
using Cinemachine;
[UnitCategory("Custom")]
public class RotateVirtualCameraNode : Unit
{
[DoNotSerialize]
public ControlInput enter { get; private set; }
[DoNotSerialize]
public ControlOutput exit { get; private set; }
[DoNotSerialize]
public ValueInput sensitivityInput { get; private set; }
[DoNotSerialize]
public ValueInput minRotationInput { get; private set; } // Minimum rotation limit
[DoNotSerialize]
public ValueInput maxRotationInput { get; private set; } // Maximum rotation limit
private float rotationX = 0f; // Track the rotation around the X axis
protected override void Definition()
{
enter = ControlInput("enter", (flow) =>
{
// Get the current game object (target object)
var targetObject = flow.stack.gameObject;
// Get the Virtual Camera from the child object
var vcam = targetObject.transform.GetChild(0).GetComponent<CinemachineVirtualCamera>();
// Get sensitivity
float sensitivity = flow.GetValue<float>(sensitivityInput);
// Get rotation limits
float minRotation = flow.GetValue<float>(minRotationInput);
float maxRotation = flow.GetValue<float>(maxRotationInput);
// Get input (mouse or touch)
float inputX, inputY;
if (Input.touchSupported && Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
inputX = touch.deltaPosition.x;
inputY = touch.deltaPosition.y;
}
else
{
inputX = Input.GetAxis("Mouse X");
inputY = Input.GetAxis("Mouse Y");
}
// Rotate around the Y axis
vcam.transform.RotateAround(targetObject.transform.position, Vector3.up, inputX * sensitivity);
// Calculate the desired rotation around the X axis
float desiredRotationX = rotationX - inputY * sensitivity;
desiredRotationX = Mathf.Clamp(desiredRotationX, minRotation, maxRotation); // Limit the rotation to the specified range
// Calculate the actual rotation difference
float rotationDifferenceX = desiredRotationX - rotationX;
// Apply the rotation around the X axis
vcam.transform.RotateAround(targetObject.transform.position, vcam.transform.right, rotationDifferenceX);
// Update the tracked rotation
rotationX = desiredRotationX;
return exit; // Continue execution
});
exit = ControlOutput("exit");
sensitivityInput = ValueInput<float>("sensitivity", 2f); // Default sensitivity
minRotationInput = ValueInput<float>("minRotation", -45f); // Default minimum rotation
maxRotationInput = ValueInput<float>("maxRotation", 45f); // Default maximum rotation
}
}