Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在单位2d中保持恒定速度?_C#_Unity3d - Fatal编程技术网

C# 如何在单位2d中保持恒定速度?

C# 如何在单位2d中保持恒定速度?,c#,unity3d,C#,Unity3d,我对Unity和C#很陌生。我正在努力开发我的第一个游戏,很多语法让我困惑。当按下“a”或“d”或箭头键时,我试图在x轴上用力移动我的精灵,但我的精灵似乎没有以恒定速度移动 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class playerMovement : MonoBehaviour { public floa

我对Unity和C#很陌生。我正在努力开发我的第一个游戏,很多语法让我困惑。当按下“a”或“d”或箭头键时,我试图在x轴上用力移动我的精灵,但我的精灵似乎没有以恒定速度移动

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

public class playerMovement : MonoBehaviour
{
    public float vel = .5f;
    public float jumpVel = 10f;
    public bool isGrounded = false;
    private Rigidbody2D rb;

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

    // Update is called once per frame
    void Update()
    {
        //Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f,0f);
        //transform.position += move * Time.deltaTime * vel;
        if(Input.GetAxisRaw("Horizontal") == 1){
            rb.AddForce(Vector2.right*vel, ForceMode2D.Force);
        }else if(Input.GetAxisRaw("Horizontal") == -1){
            rb.AddForce(Vector2.right*-vel, ForceMode2D.Force);
        }
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
            rb.velocity = Vector2.up * jumpVel;
        }

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类玩家运动:单一行为
{
公共浮动水平=0.5f;
公共浮子跳线电平=10f;
公共bool isGrounded=false;
私有刚体2d rb;
//在第一帧更新之前调用Start
void Start()
{
rb=transform.GetComponent();
}
//每帧调用一次更新
无效更新()
{
//Vector3 move=newvector3(Input.GetAxisRaw(“水平”),0f,0f;
//transform.position+=move*Time.deltaTime*vel;
if(Input.GetAxisRaw(“水平”)==1){
rb.AddForce(向量2.右*vel,ForceMode2D.力);
}else if(Input.GetAxisRaw(“水平”)=-1){
rb.AddForce(矢量2.right*-vel,ForceMode2D.Force);
}
if(Input.GetKeyDown(KeyCode.Space)和&isground==true){
rb.velocity=Vector2.up*jumpVel;
}
}
}

这是因为您使用rb.AddForce向其添加力,它在每次播放时都会增加速度,对于玩家移动,我建议使用CharacterMovement组件,但如果您想使用Rigidbody2d,我能想到的最佳解决方案是:

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

public class playerMovement : MonoBehaviour
{
    public float vel = .5f;
    public float jumpVel = 10f;
    public bool isGrounded = false;
    private Rigidbody2D rb;
    public float maxvel = 10f;

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

    // Update is called once per frame
    void Update()
    {
        //Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f,0f);
        //transform.position += move * Time.deltaTime * vel;
        if(Input.GetAxisRaw("Horizontal") == 1 &&rb.velocity.x<=maxvel){
            rb.AddForce(Vector2.right*vel, ForceMode2D.Force);
        }else if(Input.GetAxisRaw("Horizontal") == -1 && rb.velocity.x>=-maxvel){
            rb.AddForce(Vector2.right*-vel, ForceMode2D.Force);
        }
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
            rb.velocity = Vector2.up * jumpVel;
        }

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类玩家运动:单一行为
{
公共浮动水平=0.5f;
公共浮子跳线电平=10f;
公共bool isGrounded=false;
私有刚体2d rb;
公共浮点数最大值=10f;
//在第一帧更新之前调用Start
void Start()
{
rb=transform.GetComponent();
}
//每帧调用一次更新
无效更新()
{
//Vector3 move=newvector3(Input.GetAxisRaw(“水平”),0f,0f;
//transform.position+=move*Time.deltaTime*vel;
if(Input.GetAxisRaw(“水平”)==1&&rb.velocity.x=-maxvel){
rb.AddForce(矢量2.right*-vel,ForceMode2D.Force);
}
if(Input.GetKeyDown(KeyCode.Space)和&isground==true){
rb.velocity=Vector2.up*jumpVel;
}
}
}

所以,在这个答案中,float maxvel(代表最大速度),这个float用来知道你想要一个idoneus速度的位置(你应该自己调整它,我刚才给代码提供了一些参考),我也调用rb.velocity.x来检查横轴上的实际速度,我决定不直接改变速度,因为这会使它从0变为你放在那里的速度,使用addforce可以使它平稳工作

这是因为您使用rb.AddForce向其添加力,它每次播放时都会增加速度,对于玩家移动,我建议使用CharacterMovement组件,但是如果您想使用Rigidbody2d,我能想到的最佳解决方案是:

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

public class playerMovement : MonoBehaviour
{
    public float vel = .5f;
    public float jumpVel = 10f;
    public bool isGrounded = false;
    private Rigidbody2D rb;
    public float maxvel = 10f;

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

    // Update is called once per frame
    void Update()
    {
        //Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f,0f);
        //transform.position += move * Time.deltaTime * vel;
        if(Input.GetAxisRaw("Horizontal") == 1 &&rb.velocity.x<=maxvel){
            rb.AddForce(Vector2.right*vel, ForceMode2D.Force);
        }else if(Input.GetAxisRaw("Horizontal") == -1 && rb.velocity.x>=-maxvel){
            rb.AddForce(Vector2.right*-vel, ForceMode2D.Force);
        }
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
            rb.velocity = Vector2.up * jumpVel;
        }

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类玩家运动:单一行为
{
公共浮动水平=0.5f;
公共浮子跳线电平=10f;
公共bool isGrounded=false;
私有刚体2d rb;
公共浮点数最大值=10f;
//在第一帧更新之前调用Start
void Start()
{
rb=transform.GetComponent();
}
//每帧调用一次更新
无效更新()
{
//Vector3 move=newvector3(Input.GetAxisRaw(“水平”),0f,0f;
//transform.position+=move*Time.deltaTime*vel;
if(Input.GetAxisRaw(“水平”)==1&&rb.velocity.x=-maxvel){
rb.AddForce(矢量2.right*-vel,ForceMode2D.Force);
}
if(Input.GetKeyDown(KeyCode.Space)和&isground==true){
rb.velocity=Vector2.up*jumpVel;
}
}
}

所以,在这个答案中,float maxvel(代表最大速度),这个float用来知道你想要一个idoneus速度的位置(你应该自己调整它,我刚才给代码提供了一些参考),我也调用rb.velocity.x来检查横轴上的实际速度,我决定不直接改变速度,因为这会使它从0变为你放在那里的速度,使用addforce可以使它平稳工作

您可能希望更正示例中的变量声明。您将其拼错为“maxbel”。您可能希望更正示例中的变量声明。你把它拼错了“maxbel”。