【发布时间】:2021-04-26 15:11:39
【问题描述】:
所以我目前正在尝试将我的播放器对象及其脚本添加到为相机对象编写的相机脚本中。
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform target;
private Vector3 offset;
private float damping = 0.2f;
public bool canMove = true;
public PlayerMovement player;
private void Start()
{
player = gameObject.GetComponent<PlayerMovement>();
}
void FixedUpdate()
{
if (player.faceRight)
{
offset = new Vector3(1f, 0.5f, -10f);
transform.position = Vector3.Lerp(transform.position, target.position, damping) + offset;
}
else if (!player.faceRight)
{
offset = new Vector3(-1f, 0.5f, -10f);
transform.position = Vector3.Lerp(transform.position, target.position, damping) + offset;
}
}
}
我的问题是我不能写player = gameObject.Find("Player");,因为统一说的是“那些是不同类型的元素”,但如果我写public PlayerMovement player;,我可以拖动我的播放器对象并且它有效。问题是我想学习如何在不拖动对象的情况下使用它。
【问题讨论】: