当前位置:网站首页>Rotating camera
Rotating camera
2022-06-13 00:50:00 【Small fish game development】
using System;
using UnityEngine;
namespace MainEditor
{
/// <summary>
/// The magic of love goes around
/// Rotate the camera by a specified number of degrees
/// </summary>
public class RotationCamera : MonoBehaviour
{
public int weight = 8;
public float radius = 10;
public float power = 0.05f;
public int count=8;
public Action<int> onIndexChanged;
int curAngleIndex;
int curIndex;
int runState;
int stopAnale;
float perimeter;
Vector2 speed;
Vector2 worldPos;
Vector2 origin;
Vector2 direction;
Vector2 screenPos;
Vector3 eulerAngle;
Action[] inputFunctions;
Action[] runStateFunctions;
private void Awake()
{
gameObject.AddComponent<DrawRadius>().radius = radius;
stopAnale = 360 / weight;
origin = transform.position;
perimeter = 2 * Mathf.PI * radius;
inputFunctions = new Action[] {
Default, MouseDown, MouseStay, MouseUp };
runStateFunctions = new Action[] {
SpeedRoation, FixedAngle };
onIndexChanged = index => Debug.Log(index);
}
private void Update()
{
inputFunctions[GetInputState()].Invoke();
}
void Default()
{
if (runState == -1)
return;
runStateFunctions[runState].Invoke();
}
void Rotation(Vector3 vector )
{
var newValue = vector.z;
var oldValue = transform.localEulerAngles.z;
if (Mathf.Abs(newValue - oldValue) < 0.0001f)
return;
transform.localEulerAngles = vector;
if (newValue > 359)
return;
var v = (int)vector.z / stopAnale;
if (v != curAngleIndex)
{
curAngleIndex = v;
// 0 - 359
int sign = 0;
if(Mathf.Abs(newValue - oldValue) >250)
{
if(newValue<100)// Right
sign = 1;
else if(newValue >250)// Left
sign = -1;
}
else
{
if(newValue >oldValue)// Right
sign = 1;
else if(newValue < oldValue)// Left
sign = -1;
}
curIndex += sign;
if (curIndex < 0)
curIndex = count - 1;
else if (curIndex >= count)
curIndex = 0;
onIndexChanged?.Invoke(curIndex);
}
}
void SpeedRoation()
{
speed *= Mathf.Pow(0.1f, Time.deltaTime);
if (Mathf.Abs(speed.magnitude) < 10)
{
speed = Vector3.zero;
var angle = transform.localEulerAngles.z;
var v = (int)angle / stopAnale;
// Choose the middle of the portion at a fixed angle , Avoid switching to other angles with a slight shift to the left
eulerAngle = new Vector3(0, 0, v * stopAnale + stopAnale * 0.5f);
runState = 1;
}
else
{
// power To suppress or enlarge the rotation distance . If the radius is too small and the speed is too fast
var radian = speed.magnitude * power / perimeter;
var angle = Mathf.Rad2Deg * radian;
Rotation(transform.localEulerAngles + new Vector3(0, 0, angle * Mathf.Sign(direction.x)));
}
}
void FixedAngle()
{
Rotation(Vector3.Slerp(transform.localEulerAngles, eulerAngle, 0.1f));
if (Mathf.Abs(transform.localEulerAngles.z - eulerAngle.z) < 0.1f)
{
Rotation(eulerAngle);
runState = -1;
}
}
void MouseDown()
{
runState = -1;
worldPos = GetWorldPosition();
screenPos = Input.mousePosition;
}
void MouseUp()
{
Vector2 pos = GetWorldPosition();
var delta = worldPos - pos;
direction = ((Vector2)Input.mousePosition - screenPos).normalized;
speed = delta / Time.deltaTime;
worldPos = pos;
runState = 0;
}
void MouseStay()
{
Vector2 pos = GetWorldPosition();
var oldDir = (worldPos - origin).normalized * radius ;
var newDir = (pos - origin).normalized * radius ;
// seek 2 Radian value between points
var radian = Mathf.Atan2(newDir.y, newDir.x) - Mathf.Atan2(oldDir.y, oldDir.x);
var angle = Mathf.Rad2Deg * radian;
Rotation(transform.localEulerAngles + new Vector3(0, 0, -angle));
// Because the rotation is updated , So we need to get the coordinates again
worldPos = GetWorldPosition();
}
int GetInputState()
{
if (Input.GetMouseButtonDown(0))
return 1;
if (Input.GetMouseButton(0))
return 2;
if (Input.GetMouseButtonUp(0))
return 3;
return 0;
}
Vector3 GetWorldPosition()
{
return Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
}
边栏推荐
- Easyexcel read excel simple demo
- . The way to prove the effect of throwing exceptions on performance in. Net core
- Canvas game 2048 free map size
- 三栏简约typecho主题Lanstar/蓝星typecho主题
- Kotlin coroutine suspend function suspend keyword
- Buuctf's babysql[geek challenge 2019]
- Hard (magnetic) disk (II)
- sort
- Kotlin 协程,job的生命周期
- Map from getting started to performance optimization
猜你喜欢

Mysql批量插入数据时如何解决重复问题?

Maybe we can figure out the essence of the Internet after the dust falls

Canvas game 2048 free map size

Hard (magnetic) disk (I)

A simple deadlock example

MySQL locates the position of the character in the string String substitution

People and gods are angry. Details of Tangshan "mass beating of women incident"

Canvas random bubbling background

Undirected graph -- computing the degree of a node in compressed storage
![[network protocol] problems and solutions in the use of LwIP](/img/25/d064a761724936b8f35ee0c779e597.jpg)
[network protocol] problems and solutions in the use of LwIP
随机推荐
1. Google grpc framework source code analysis Hello World
How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
Influence of higher order poles on waveform
Comparison of disk partition modes (MBR and GPT)
[JS component] floating text
通过抓包下载钉钉直播回放
Buuctf's babysql[geek challenge 2019]
The seventh finals of the Blue Bridge Cup
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
今日在家休息
也许尘埃落地,我们才能想清楚互联网的本质
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
Druid reports an error connection holder is null
Introduction to ROS from introduction to mastery (zero) tutorial
Bubble sort - alternate sort at both ends
Win10 home vs pro vs enterprise vs enterprise LTSC
Easyexcel read excel simple demo
Kotlin 协程挂起函数 suspend 关键字
[virtual machine] notes on virtual machine environment problems
pytorch和tensorflow有什么区别?