摄像机的跟随

开发摄像机的跟随
•使用Vertor3.Lerp 插值运算
摄像机的视角,让摄像机观察着角色
•Quaternion.LookRotation 得到一个四元数,表示跟参数一样方向的旋转
•Quaternion.Slerp 按照圆弧插值,这个插值计算更适用于,角度变换


首先调整摄像机,使它和player在一个合适角度和位置,然后选择GameObject-Align With View,锁定视角,把Camera拖动到Player下,以Player为本地坐标,记录下此时Camera的Y轴和Z轴。记录完之后把Camera拖出来恢复。,接下来到代码时间。
摄像机如何找到Player,可以通过标签找到Player,把Player的Tag设置为Player,定义一个名为Tags的C#脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tags {
    public const string player = "Player";
	
}
再定义一个名为fllowplayer的c#的脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class fllowplayer : MonoBehaviour {
    private Transform player;
    public float speed = 2;
	// Use this for initialization
	void Start () {
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;
	}
	
	// Update is called once per frame
	void Update () {
        Vector3 targetPos = player.position + new Vector3(0, 1.75f, -3.42f);//前面记录下的Y轴和Z轴的值.
        transform.position = Vector3.Lerp(transform.position, targetPos, speed * Time.deltaTime);
        Quaternion targetRotation = Quaternion.LookRotation(player.position-transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值