一.第三人称视角 _1

先设置好相机与玩家之间的角度

unity3d之相机跟随人物-LMLPHP

给相机添加代码

 using UnityEngine;
using System.Collections; namespace CompleteProject
{
public class CameraFollow : MonoBehaviour
{
public Transform target; // The position that that camera will be following.
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);
}
}
}

二、第三人称视角_2,可自己根据游戏效果调节位置

在inspector面板修改mHeight与mDistance值即可

 using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class FollowCamera : MonoBehaviour
{
public Transform mTarget; //相机的跟随目标
public float mHeight=8f;
public float mDistance=6f;
public float interpolation = 20.0f;
public Space space = Space.World; // Use this for initialization
void Start ()
{ } // Update is called once per frame
void LateUpdate ()
{
if (mTarget)
{
Vector3 dest = Vector3.zero;
switch (space)
{
case Space.World://以世界为中心
dest = mTarget.position + Vector3.up * mHeight - Vector3.forward * mDistance;
break;
case Space.Self://玩家为中心,一般用不到可省略switch
dest = mTarget.position + mTarget.up * mHeight - mTarget.forward * mDistance;
break;
default:
break;
}
transform.position = Vector3.Lerp(transform.position, dest, interpolation);
transform.LookAt(mTarget.position);
}
}
}
05-11 23:01