在Unity中用触摸式C#改变重力。

问题描述 投票:0回答:1

我不会做一个游戏,其中重力改变形式1。请在此输入图片描述 至-1请在此输入图片描述 当我触碰一个按钮时,我就会回到另一个按钮。一开始可以用,但后来就不工作了。

using System.Collections;
 using System.Collections.Generic;
   using UnityEngine;

       public class Button : MonoBehaviour
     {
       private Rigidbody2D rb;

       private bool moveUp;
     private bool MoveDown;

   // Start is called before the first frame update
     void Start()
     {
       rb = GetComponent<Rigidbody2D>();

      moveUp = false;
     MoveDown = false;

    }


         public void PionterDownRight(){
        moveUp = true;
          }
          public void PionterUpRight(){
           moveUp=false;
           }

       public void PionterDownLeft(){
          MoveDown= true;
          }
         public void PionterUpLeft(){
        MoveDown = false;
          }

     // Update is called once per frame
void Update()
{
 if(moveUp){
  rb.gravityScale = -1;
 }

 if (MoveDown){
       rb.gravityScale = 1;
 }





}

}

c# unity3d gravity
1个回答
0
投票

我建议只有一个bool变量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Button : MonoBehaviour
{
    private Rigidbody2D rb;

    private bool moveDown = true;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }


    public void PionterDownRight()
    {
        moveDown = false;
    }
    public void PionterUpRight()
    {
        moveDown = true;
    }

    public void PionterDownLeft()
    {
        moveDown = true;
    }
    public void PionterUpLeft()
    {
        moveDown = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (moveDown == true)
        {
            rb.gravityScale = 1;
        }
        else
        {
            rb.gravityScale = -1;
        }
    }
}

0
投票

Graviyscale会影响到重力对刚体的影响程度,我认为它没有工作,因为它在0=没有影响的情况下是稳定的。你可能需要采取不同的方法,比如说

rb.gravityScale = -1;

换成

rb.AddForce(new Vector2(0, 9.81f * 2));
© www.soinside.com 2019 - 2024. All rights reserved.