码迷,mamicode.com
首页 > 编程语言 > 详细

unity开发c#代码

时间:2018-09-20 01:14:43      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:相减   常见   简单   注意   player   this   ini   pre   span   

1.摄像头跟随主角移动,并支持旋转。

开发过程中需要摄像头以一定距离跟随player,同时会进行旋转,属于一种常见的跟随方式。

using UnityEngine;
using System.Collections;

public class CameriaTrack : MonoBehaviour {
    private Vector3 offset = new Vector3(0,5,4);//相机相对于玩家的位置
    private Transform target;
    private Vector3 pos;

    public float speed = 2;

    // Use this for initialization
    void Start () {
        target = GameObject.FindGameObjectWithTag("Player").transform;

    }

    // Update is called once per frame
    void Update () {
        pos = target.position + offset;
        this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//调整相机与玩家之间的距离
        Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//获取旋转角度
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);

    }
}

这里值得注意的是:需要记得将主角的tag设置为player。

原理非常简单,一直将摄像机和玩家距离控制在一个值之内,旋转角度上相减取差值即可。

觉得位置不太好的话,改一下最开始的offset即可。同时别忘了吧脚本挂在摄像头上。

unity开发c#代码

标签:相减   常见   简单   注意   player   this   ini   pre   span   

原文地址:https://www.cnblogs.com/lixiaoyao123/p/9678201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!