如何在作为游戏对象子项的新相机中应用脚本

How can I apply scripts in new camera which is child of a gameobject

早些时候我遇到了关于 unity camera 的问题,它总是卡在 0,0,0.08 上,我也找到了解决方案,所以我首先创建一个空的游戏对象,然后将相机拖到那个空的游戏对象中,但是在这样做之后我应用于游戏对象的脚本工作正常,但我放在相机中的脚本根本不起作用

相机脚本

public float MovementAmplitude = 0.1f;
public float MovementFrequency = 2.25f;
void Update()
{
    transform.position = new Vector3(
         transform.position.x,
         Mathf.Cos(transform.position.z * MovementFrequency) * MovementAmplitude,
         transform.position.z

        );
}

播放器脚本

public float speed = 4.5f;
public float JumpingForcec = 450f;
void Update()
{
    transform.position += speed * Vector3.forward * Time.deltaTime;
    if (Input.GetKeyDown("space"))
    {
        Debug.Log("SPace is pressed");
        Debug.Log(GetComponent<Rigidbody>());
        GetComponent<Rigidbody>().AddForce(Vector3.up * JumpingForcec);
    }
}

尝试将所有更新内容放在同一个方法中,它应该可以同时工作(理论上,未经测试)所以您必须修复您的代码才能获得您想要的内容:

void Update() {
    // Camera update
    transform.position = new Vector3(
        transform.position.x,
        Mathf.Cos(transform.position.z * MovementFrequency) * MovementAmplitude,
        transform.position.z
    );
    // Player update
    transform.position += speed * Vector3.forward * Time.deltaTime;
    if (Input.GetKeyDown("space"))
    {
        Debug.Log("SPace is pressed");
        Debug.Log(GetComponent<Rigidbody>());
        GetComponent<Rigidbody>().AddForce(Vector3.up * JumpingForcec);
    }
}

希望对你有帮助,干杯!

首先,在处理 Rigidbody(或一般的物理)时,您不应直接通过 Transform 组件设置位置,而应使用 Rigidbody.position or in your case for a smooth movement even rather Rigidbody.MovePosition, both in FixedUpdate

一般来说,与物理相关的任何事情(所有使用 Rigidbody 的事情)都应该在 FixedUpdate 中完成,而 GetKeyDown 的检查必须在 [=20= 中完成].

PlayerScript

public class PlayerScript : MonoBehaviour
{
    public float speed = 4.5f;
    public float JumpingForcec = 450f;

    // If possible reference this in the Inspector already
    [SerializeField] private Rigidbody rigidBody;

    private bool jumpWasPressed;

    private void Awake()
    {
        if (!rigidBody) rigidBody = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        rigidBody.MovePosition(transform.position + speed * Vector3.forward * Time.deltaTime);

        if (!jumpWasPressed) return;

        Debug.Log("SPace was pressed");

        rigidBody.AddForce(Vector3.up * JumpingForcec);

        jumpWasPressed = false;
    }

    private void Update()
    {
        // Note that currently you can multijump .. later you will want to add 
        // an additional check if you already jump again
        if (Input.GetKeyDown(KeyCode.Space)) jumpWasPressed = true;
    }
}

确保 Is KinematicRigidbody 组件中被 禁用 !否则AddForce不处理

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.

我将移动到 LateUpdate 的相机移动,以确保它是在其他 Update 调用完成后计算的最后一件事。特别是在处理完所有用户输入之后(在您的情况下可能不那么相关,因为移动是在 FixedUpdate 中处理的,但一般来说)。

第二个问题:这里你没有通过跳跃来考虑改变的 Y 位置,所以而是将 "wobbling" 效果添加到玩家的 transform.position.y 中,而是使用相机的 localPosition:

public class CameraScript : MonoBehaviour
{
    public float MovementAmplitude = 0.1f;
    public float MovementFrequency = 2.25f;

    // reference the player object here
    public Transform playerTransform;

    private float originalLocalPosY;

    private void Start()
    {
        if(!playerTransform) playerTransform = transform.parent;
        originalLocalPosY = transform.localPosition.y;
    }

    private void LateUpdate()
    {
        transform.localPosition = Vector3.up * (originalLocalPosY + Mathf.Cos(playerTransform.position.z * MovementFrequency) * MovementAmplitude);
    }
}

也许你想在稍后的跳跃中禁用摆动效果;)