Initial commit
This commit is contained in:
58
Assets/Scripts/SwingSmoothRotate.cs
Normal file
58
Assets/Scripts/SwingSmoothRotate.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SwingSmoothRotate : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform swing;
|
||||
|
||||
[SerializeField] private float leftAngle = -2.457f;
|
||||
[SerializeField] private float rightAngle = 21f;
|
||||
[SerializeField] private float rotateSpeed = 60f;
|
||||
|
||||
private float targetAngleX;
|
||||
private bool rotate = false;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!rotate) return;
|
||||
|
||||
Vector3 current = swing.localEulerAngles;
|
||||
float currentX = NormalizeAngle(current.x);
|
||||
|
||||
float newX = Mathf.MoveTowards(
|
||||
currentX,
|
||||
targetAngleX,
|
||||
rotateSpeed * Time.deltaTime
|
||||
);
|
||||
|
||||
swing.localEulerAngles = new Vector3(newX, current.y, current.z);
|
||||
|
||||
if (Mathf.Abs(newX - targetAngleX) < 0.1f)
|
||||
{
|
||||
rotate = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!other.CompareTag("Player")) return;
|
||||
|
||||
if (gameObject.name.Contains("Left"))
|
||||
{
|
||||
targetAngleX = leftAngle;
|
||||
rotate = true;
|
||||
}
|
||||
else if (gameObject.name.Contains("Right"))
|
||||
{
|
||||
targetAngleX = rightAngle;
|
||||
rotate = true;
|
||||
}
|
||||
}
|
||||
|
||||
private float NormalizeAngle(float angle)
|
||||
{
|
||||
if (angle > 180f) angle -= 360f;
|
||||
return angle;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user