我制作了一个带有标签的玩家预制件,该标签在游戏开始时在场景中生成。如何让摄像机使用播放器标签跟随玩家。
当前使用以下脚本
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);
}
使用这个
如
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()
{
try
{
target = GameObject.FindGameObjectWithTag("Player").transform; // this is goint to find a certain tagged object from hirarchey and assing it to target.
}
catch (NullReferenceException ex)
{
Debug.Log("target gameObjects is not present in hierarchy ");
}
// 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);
}
或者您可以创建一个事件,并在游戏在特定时间生成时找到带有标签的游戏Obejct
如
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;
}
// Call this method where you spawing your target and set the tag and call this mehtod supply tag parameter
public void FindTaggedGameObject(string tag)
{
try
{
target = GameObject.FindGameObjectWithTag("Player").transform; // this is goint to find a certain tagged object from hirarchey and assing it to target.
}
catch (NullReferenceException ex)
{
Debug.Log("target gameObjects is not present in hierarchy ");
}
}
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);
}