C# 我如何让用户选择摄像机是否在播放器后面?
这是使相机跟随播放器的原始代码:C# 我如何让用户选择摄像机是否在播放器后面?,c#,unity3d,unity5,C#,Unity3d,Unity5,这是使相机跟随播放器的原始代码: using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { public GameObject objectToFollow; //Public variable to store a reference to the player game object private Vector3 offset;
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
private Vector3 offset; //Private variable to store the offset distance between the player and camera
// Use this for initialization
void Start()
{
//Calculate and store the offset value by getting the distance between the player's position and camera's position.
offset = transform.position - objectToFollow.transform.position;
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
// Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
transform.position = objectToFollow.transform.position + offset;
transform.LookAt(objectToFollow.transform);
}
}
这就是我试图做的,但是玩家自己(第三个人控制者)并没有根据他移动的方向旋转
根据上面的原始脚本,他做到了
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject objectToFollow; //Public variable to store a reference to the player game object
public bool behindPlayer = false;
private Vector3 cameraStartPos;
// Use this for initialization
void Start()
{
cameraStartPos = transform.position;
// Put the camera behind the player
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
}
private void Update()
{
if (behindPlayer == true)
{
transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2));
}
else
{
transform.position = cameraStartPos;
}
}
// LateUpdate is called after Update each frame
void LateUpdate()
{
transform.position = objectToFollow.transform.position;
transform.LookAt(objectToFollow.transform);
}
}
我想,相机将自动背后的球员已经没有必要改变相机的位置,如果用户想使用bool变量
但现在的脚本与上面的不同:
使用最新更新中的此行:
transform.position = objectToFollow.transform.position;
使用bool false/true,根本不会改变摄像机的位置。
没有这条线,玩家将移动,相机将跟随,但玩家不会旋转,也不会像上面那样移动
我想要的与第一个脚本一样,但带有behindPlayer变量。工作脚本:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private Vector3 offsetPosition;
[SerializeField]
private Space offsetPositionSpace = Space.Self;
[SerializeField]
private bool lookAt = true;
private void Update()
{
Refresh();
}
public void Refresh()
{
if (target == null)
{
Debug.LogWarning("Missing target ref !", this);
return;
}
// compute position
if (offsetPositionSpace == Space.Self)
{
transform.position = target.TransformPoint(offsetPosition);
}
else
{
transform.position = target.position + offsetPosition;
}
// compute rotation
if (lookAt)
{
transform.LookAt(target);
}
else
{
transform.rotation = target.rotation;
}
}
}
在inspector中,例如将偏移设置为0,5,-12,并将其设置为目标设置,例如ThirdPersonController。并将脚本附加到摄影机(如果目标是ThirdPersonController,则必须将摄影机标记为Main Camera,因为ThirdPersonController ThirdPersonUserControl脚本正在查找主摄影机)
摄像机不必是目标的子对象,摄像机可以位于层次结构中的任何位置
不管怎么说,它起作用了