簡體   English   中英

如何在新的相機中應用腳本,該相機是游戲對象的子代

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

早些時候我遇到了關於統一攝像機問題的問題,該問題總是卡在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或在您的情況下平穩的運動甚至有點Rigidbody.MovePosition ,無論是在FixedUpdate

在涉及到物理學一般什么(所以也利用一切手段, Rigidbody )應該在做FixedUpdate而檢查GetKeyDown在做Update

播放器腳本

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 Kinematic禁用 Rigidbody組件! 否則,不會處理AddForce

如果啟用 isKinematic ,則力,碰撞或關節將不再影響剛體。

為了確保它是在其他Update調用完成之后計算出的最后一件事,我將鏡頭移動到LateUpdate 尤其是在處理FixedUpdate所有用戶輸入之后(在您的情況下,由於移動是在FixedUpdate中進行的, FixedUpdate可能並不重要,但通常如此)。

第二個問題:在這里,您沒有通過跳轉來考慮更改后的Y位置,而是在播放器的transform.position.y添加“擺動”效果,而對Camera使用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);
    }
}

不過,也許您想在稍后的跳躍過程中禁用擺動效果;)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM