概述
一、摄像机控制
1.摄像机与物体同步移动
public class CameraController : MonoBehaviour {
// store a public reference to the Player game object, so we can refer to it's Transform
public GameObject player;
// Store a Vector3 offset from the player (a distance to place the camera from the player at all times)
private Vector3 offset;
// At the start of the game..
void Start ()
{
// Create an offset by subtracting the Camera's position from the player's position
offset = transform.position - player.transform.position;
}
// After the standard 'Update()' loop runs, and just before each frame is rendered..
void LateUpdate ()
{
// Set the position of the Camera (the game object this script is attached to)
// to the player's position, plus the offset amount
transform.position = player.transform.position + offset;
}
}
2.摄像机跟随物体移动
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime
最后
以上就是忧虑水杯为你收集整理的Unity基础之代码篇 —— 摄像机控制、物体控制、触发器、射线、导航代理等的全部内容,希望文章能够帮你解决Unity基础之代码篇 —— 摄像机控制、物体控制、触发器、射线、导航代理等所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复