当前位置:网站首页>[unity3d] camera follow
[unity3d] camera follow
2022-06-28 18:46:00 【little_ fat_ sheep】
1 Preface
The camera follows Camera means always following a particular GameObject , There are the following 2 Kinds of following effect :
- Position follows : The vector that the camera points to the target GameObject is always the same
- Position and attitude follow : The coordinates and orientation of the camera in the coordinate system of the target game object remain unchanged

The simplest way to follow the camera position and attitude is : Drag the camera GameObject under the target GameObject , That is, the camera is the sub object of the target game object , The principle is : The position and posture of the child object always change with the change of the parent object , And remain relatively unchanged . This article does not show this approach , The camera position transformation will be controlled by code , Realize the camera following effect .
2 application
2.1 Position follows
1) The game object
The object of the game Transform The component parameters are as follows :
| Name | Type | Position | Rotation | Scale | Color/Texture |
|---|---|---|---|---|---|
| Plane | Plane | (0, 0, 0) | (0, 0, 0) | (10, 10, 10) | GrassRockyAlbedo |
| Tank | Empty | (0, 0.25, -5) | (0, 0, 0) | (1, 1, 1) | —— |
| Button | Cube | (0, 0, 0) | (0, 0, 0) | (2, 0.5, 2) | #228439FF |
| Top | Cube | (0, 0.5, 0) | (0, 0, 0) | (1, 0.5, 1) | #228439FF |
| Gun | Cylinder | (0, 0, 1.5) | (90, 0, 0) | (0.2, 1, 0.4) | #228439FF |
The hierarchy of the game object is as follows :

2) Script components
Tank.cs
using UnityEngine;
public class Tank : MonoBehaviour {
void Update () {
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
transform.Translate(0, 0, ver * Time.deltaTime * 3);
transform.Rotate(Vector3.up * hor * Time.deltaTime * 120f);
}
}explain :Tank.cs The script component is hung on Tank On the object of the game .
CameraFollow.cs
using UnityEngine;
public class CameraFollow : MonoBehaviour {
private Transform player; // The player
private Vector3 dir; // The direction vector from the initial camera to the target tank
void Start() {
player = GameObject.Find("Tank").transform;
dir = player.position - transform.position;
}
void Update () {
transform.position = player.position - dir;
}
}explain :CameraFollow.cs The script component is hung on Main Camera On the object of the game .
3) Running effect

2.2 Position and attitude follow
The game object is its Transform Component parameters are the same as 2.1 section ,.
1) Script components
Tank.cs Script components are the same as 2.1 section .
CameraFollow.cs
using UnityEngine;
public class CameraFollow : MonoBehaviour {
private Transform player; // The player
private Vector3 dir; // The direction vector from the initial camera to the target tank
void Start() {
player = GameObject.Find("Tank").transform;
dir = player.position - transform.position;
}
void Update () {
transform.position = transformVecter(-dir, player.position, player.right, player.up, player.forward);
transform.rotation = Quaternion.LookRotation(player.position - transform.position);
}
// Seeking for origin Origin , locX, locY, locZ Is the vector in the local coordinate system of the coordinate axis vec The corresponding vector in the world coordinate system
private Vector3 transformVecter(Vector3 vec, Vector3 origin, Vector3 locX, Vector3 locY, Vector3 locZ) {
return vec.x * locX + vec.y * locY + vec.z * locZ + origin;
}
}2) Running effect

边栏推荐
猜你喜欢

使用Karmada实现Helm应用的跨集群部署

Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years

180.1.连续登录N天(数据库)

324. swing sequencing II

Advanced technology management - how managers communicate performance and control risks

MindSpore系列一加载图像分类数据集

1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import

Concept and code implementation of heap

匿名函数this指向以及变量提升

内存泄露
随机推荐
Chapter 2 processing files, cameras and GUI Cameo applications
Lumiprobe非荧光叠丨氮化物研究丨3-叠丨氮丙醇
Oom out of memory memory overflow
匿名函数this指向以及变量提升
select/poll/epoll
微软独家付费功能,也被完美解锁了
ONEFLOW source code parsing: automatic inference of operator signature
Go descending sort takes top n
Concept and code implementation of heap
东方财富软件股票开户是靠谱的吗?在哪开户安全
Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络
Lumiprobe ProteOrange 蛋白质凝胶染料说明书
浦发银行软件测试面试真题(小编面试亲测)
数据库对比工具
Michael Wooldridge, professeur à l'Université d'Oxford: comment la communauté de l'IA voit les réseaux neuronaux depuis près de 40 ans
Native implementation Net5.0+ custom log
牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
使用Karmada实现Helm应用的跨集群部署