当前位置:网站首页>Unity lock the arc motion of the camera around the locked target
Unity lock the arc motion of the camera around the locked target
2022-07-21 23:19:00 【A shuttle keyboard, Ren lifetime】
Recently in use unity Make one mmorpg game
Among them, the planning of camera movement raises a lot of demands
Made many versions The final version Make a note of :
The general demand is like this
When the camera is at point A when , The character locks the target camera from A Point is role centered , Circular motion to B spot
B The point is the reverse extension line of the connecting line between the character and the target , The camera motion speed is required to be controllable
The code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test1 : MonoBehaviour {
/// <summary>
/// role
/// </summary>
public Transform target;
/// <summary>
/// The goal is
/// </summary>
public Transform target2;
/// <summary>
/// The camera
/// </summary>
public Camera camera;
/// <summary>
/// The horizontal distance between the camera and the character
/// </summary>
public float _radius;
/// <summary>
/// How many meters is the camera looking directly in front of the character
/// </summary>
public float cameraLookDis;
/// <summary>
/// The vertical distance between the camera and the character
/// </summary>
public float _camHeight;
/// <summary>
/// Camera rotation angular velocity change curve
/// </summary>
public AnimationCurve _cameraAngleSpeedCurve;
/// <summary>
/// Camera angular velocity
/// </summary>
public float cameraSpeed;
public bool reset;
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
if(reset) {
Vector3 pos = camera.transform.position;
// Character to target vector
Vector3 dir = target.position - target2.position;
dir.y = 0.0f;
// The horizontal vector from the character to the camera
Vector3 roted = dir.normalized * _radius;
// Where the camera looks
Vector3 lookat = target.position - dir.normalized * cameraLookDis;
// The camera should move to the position on the plane
Vector3 endPos = lookat + roted;
// The position of the current camera on the plane after the horizontal distance between the calibration and the character
Vector3 disEndPos = GetDisPos(pos, lookat);
Vector3 curPos = endPos;
// The current position and the angle to the position (+-180)
float angle = Angle_180(disEndPos - lookat, endPos - lookat);
// Obtain the speed multiplier on the curve according to the angle
float curSpeedRate = _cameraAngleSpeedCurve.Evaluate(Mathf.Abs(angle));
float curSpeed = cameraSpeed;
// Adjust the rotation direction according to the angle
if(angle > 0) curSpeed *= 1;
else if(angle < 0) curSpeed *= -1;
// Adjust the position according to the angle
if(Mathf.Abs(angle) > Mathf.Abs(curSpeed * Time.deltaTime * curSpeedRate)) {
float rotAngle = curSpeed * Time.deltaTime * curSpeedRate;
// Calculate the position to which the rotation angle should arrive according to the angle radius of the center of the circle
curPos = GetCurPos(target.position, disEndPos, rotAngle);
}
// The camera looks at the target
camera.transform.LookAt(lookat);
// Camera position assignment
camera.transform.position = curPos + Vector3.up * (_camHeight + target.position.y);
}
}
/// <summary>
/// Get the position after the standard distance of the current angle
/// </summary>
/// <returns></returns>
private Vector3 GetDisPos(Vector3 pos,Vector3 _lookat) {
Vector3 view = _lookat - pos;
view.y = 0.0f;
view.Normalize();
Vector3 endPos = _lookat - view * _radius;
endPos.y = 0f;
return endPos;
}
private Vector3 GetCurPos(Vector3 center, Vector3 curPos, float angle) {
return RotateRound(curPos, center, Vector3.up, angle);
}
/// <summary>
/// Rotate a specified angle around a point
/// </summary>
/// <param name="position"> Self coordinate </param>
/// <param name="center"> Center of rotation </param>
/// <param name="axis"> Around the axis of rotation </param>
/// <param name="angle"> Rotation Angle </param>
/// <returns></returns>
private Vector3 RotateRound(Vector3 position, Vector3 center, Vector3 axis, float angle) {
return Quaternion.AngleAxis(angle, axis) * (position - center) + center;
}
/// <summary>
/// Find the angle between two vectors +-180
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <returns></returns>
public float Angle_180(Vector3 from, Vector3 to) {
from.y = 0; to.y = 0;
Vector3 v3 = Vector3.Cross(from, to);
if(v3.y > 0)
return Vector3.Angle(from, to);
else
return -Vector3.Angle(from, to);
}
}
The effect of the current code is like this
When there is a locked target It ensures that the camera can illuminate the character and target at the same time
Ensure that the character rushes to the goal And to the rear of the target The camera can still get out of the curve
Will not suddenly turn over It's very abrupt
Parameters can adjust the angular velocity of the camera moving around the character
The angular velocity of the camera between different angle ranges can be adjusted through Bezier curve
It can be adjusted to the effect similar to interpolation
Of course, this only applies to our current needs
Just send a document to record your implementation process
边栏推荐
猜你喜欢
随机推荐
HCIA_OSPF实验
OSPF实验
Hcip day 8
Hcip day 1
OSPF工作过程及其简单实验
HCIP MGRE综合实验
Comprehensive experiment of P2P network and virtual private line
AGVmir205--软件开发(API调用)
HCIA
Hcip second day comprehensive experiment
OSPF working process and its simple experiment
rip綜合實驗
Parameters of function
Properties文件的读取和写入
OSPF optimization
Rip routing information protocol
Rip comprehensive experiment
[untitled] hcip first day notes
OSPF routing control
Dynamic routing ---rip