簡體   English   中英

基本的C#Unity2D運動腳本不起作用

[英]Basic C# Unity2D movement script not functioning

嗨,各位同花異草! 我最近進入了Unity中的C#,因為我相信它比UnityScript具有更多的功能,並且我來自C ++背景。 引用示例資產中2D腳本中的一些代碼,我嘗試創建自己的代碼。 代碼如下:

    using UnityEngine;

public class PlayerControl : MonoBehaviour {
//Variables
    [HideInInspector]
    public bool facingForward = true; //for Flip() Function
    bool isGround = true; //for Grounded() Function
    public float maxSpeed = 5.0f; //Terminal sideways velocity
    public float HorizonAxis; //Checks for a/d movement 
    public float jumpFloat = 1000.0f; //Modular, use Unity Editor
    public float moveFloat = 400.0f; // "                       "

    void Start() {

        //transform.position(0,0,0);

    }

    void Flip() {
        facingForward = !facingForward; //switches boolean
        Vector3 theScale = transform.localScale; //assigns vector to localscale of Player
        theScale.x *= -1; //if x = 1, position becomes -1 and thus flips
        transform.localScale = theScale; //reassigns the localscale to update theScale
    }
    bool Grounded() {
        if (transform.position.y > 1) { //if position of gameObject is greater that 1, not grounded
            isGround = false;
        } else {
            isGround = true;
        }
        return isGround; //function returns true or false for isGround
    }

    void Update() {
        HorizonAxis = /*UnityEngine.*/Input.GetAxis ("Horizontal"); //assigns HorizonAxis to a/d movement from UnityEngine.Input
        if (HorizonAxis * rigidbody2D.velocity.x > maxSpeed) { // if Input a/d by current x velocity of gameObject is greater than maxSpeed             
        rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y); //1 or -1 times the max speed, depending on direction
    }

    else if (HorizonAxis * rigidbody2D.velocity.x < maxSpeed) { //if Input a/d is less than terminal velocity
        rigidbody2D.AddForce(Vector2.right * HorizonAxis * moveFloat); //add force to the right equivilant to Input by scalar moveFloat
    }
        if (Input.GetButtonDown ("Jump")) { //If Space
            if(isGround) { //and isGround returns true
                rigidbody2D.AddForce(new Vector2(0.0f, jumpFloat)); //add upwards force to bottom of rigidbody2D
                isGround = false; //Resets isGround value
            }
        }
        if (HorizonAxis > 0 && !facingForward) {//if UnityEngine.Input is to the right and facing left
            Flip (); //execute
        }
        else if (HorizonAxis < 0 && facingForward) { //else
                    Flip (); //execute 
        }

    }
}

不幸的是,該代碼無法正常工作。 我沒有編譯錯誤,但是任何Input都不會影響字符的當前位置。 我應該使用transform.Translate更改位置,還是將AddForce堅持使用Vector2,直到角色達到maxSpeed為止?

謝謝堆:)

我(很高興)解決了跳動問題,基本上,您在Update()函數中查找輸入,該函數在FixedUpdate()中切換了布爾值

void Update () {
    if (Input.GetKeyDown (KeyCode.Space) && playerGrounded) {
        playerJumped = true;
    }
}

然后在FixedUpdate()中,我尋找了這個布爾並做了我的AddForce

void FixedUpdate () {
    if (playerJumped) {
        playerJumped = false;
        rigidbody2D.AddForce (new Vector2 (0, jumpForce),ForceMode2D.Impulse);
        playerGrounded = false;
    }
}

將playerJumped設置為false可確保它不會運行幾次,並且播放器不會再次跳動,因為我還將接地設置為false。 當玩家與標記為“地面”的事物發生碰撞時,grounded會恢復為true。

總體而言,我還是Unity和C#的新手,所以我不能保證這是最好的方法。 希望我能有所幫助;)

暫無
暫無

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

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