1

unity5 を使用してオンラインでスペース シューターのチュートリアルを実行しようとしていますが、rigidbody に問題があります。

Rigidbody が Component.GetComponent() に置き換えられたことに気づきましたが、これをすべて入力する代わりに変数を作成したいと思います。

Component.GetComponent() を使用して大量のエラーが発生し、何が問題なのかわかりません。

これが私のコードスニペットです。クランプで動きを制限しようとしています:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;
    public float xMin, zMin, xMax, zMax;

    void FixedUpdate(){
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        Component.GetComponent<Rigidbody>().velocity = movement*speed;

        Component.GetComponent<Rigidbody>().position = new Vector3
        (
            Mathf.Clamp(Component.GetComponent<Rigidbody>().position.x, xMin, xMax),
            0.0f,
            Mathf.Clamp(Component.GetComponent<Rigidbody>().position.z, zMin, zMax)
        );
    }
}

そして、これが私に与えるエラーのスピットロードです:

Finished updating scripts / assemblies

Some scripts have compilation errors which may prevent obsolete API usages to get updated. Obsolete API updating will continue automatically after these errors get fixed.

Assets/Scripts/PlayerController.cs(14,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'

Assets/Scripts/PlayerController.cs(14,47): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'

Assets/Scripts/PlayerController.cs(18,31): error CS1502: The best overloaded method match for `UnityEngine.Mathf.Clamp(float, float, float)' has some invalid arguments

Assets/Scripts/PlayerController.cs(18,31): error CS1503: Argument `#1' cannot convert `object' expression to type `float'

Assets/Scripts/PlayerController.cs(20,47): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'

Assets/Scripts/PlayerController.cs(20,31): error CS1502: The best overloaded method match for `UnityEngine.Mathf.Clamp(float, float, float)' has some invalid arguments

Assets/Scripts/PlayerController.cs(20,31): error CS1503: Argument `#1' cannot convert `object' expression to type `float'

Assets/Scripts/PlayerController.cs(21,18): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments

Assets/Scripts/PlayerController.cs(21,18): error CS1503: Argument `#1' cannot convert `object' expression to type `float'

Assets/Scripts/PlayerController.cs(16,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'

これは非常に多くのエラーを保証するほど多くのコードではないため、重要で明白な何かが欠けているように感じます。

4

1 に答える 1

1

非静的クラス関数を使用する前に、まずオブジェクトのインスタンスを作成する必要があります。

あなたの場合、RigidBody コンポーネントがアタッチされている gameObject になる可能性が最も高いです。コード例は次のとおりです。

gameObject.GetComponent<RigidBody>().velocity = movement * speed;

コード内の他の文字列をそれぞれやり直します。

于 2015-07-11T07:10:18.683 に答える