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
C# 当按下或轻触按钮时,如何使角色移动一定距离_C#_Unity3d_2d Games - Fatal编程技术网

C# 当按下或轻触按钮时,如何使角色移动一定距离

C# 当按下或轻触按钮时,如何使角色移动一定距离,c#,unity3d,2d-games,C#,Unity3d,2d Games,所以我现在正在做一个2D游戏,只是为了学习与制作2D游戏相关的不同东西或代码。 所以我遇到了一个麻烦,我很好奇如何让角色移动一定的距离,比如说每点击一次按钮就移动一个街区。我在这里举个例子。把网格想象成一块土地 移动一定距离 这是我的角色用我当前的代码所做的 这是我的动作代码 using System.Collections; using System.Collections.Generic; using UnityEngine; public class keycontrol : MonoB

所以我现在正在做一个2D游戏,只是为了学习与制作2D游戏相关的不同东西或代码。 所以我遇到了一个麻烦,我很好奇如何让角色移动一定的距离,比如说每点击一次按钮就移动一个街区。我在这里举个例子。把网格想象成一块土地

移动一定距离

这是我的角色用我当前的代码所做的 这是我的动作代码

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

public class keycontrol : MonoBehaviour
{

    private float moveSpeed;
    private Rigidbody2D rb2d;
    private Vector2 change;
    private Animator animator;
    bool isXMoving;
    bool isYMoving;


    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        change.x = Input.GetAxisRaw("Horizontal");
        change.y = Input.GetAxisRaw("Vertical");
        
    if (Mathf.Abs(change.x) > Mathf.Abs(change.y))
    {
        change.y = 0;
    }
    else
    {
        change.x = 0;
    }

        animator.SetFloat("walk_right", change.x);
        animator.SetFloat("walk_left", -change.x);
        animator.SetFloat("walk_down", -change.y);
        animator.SetFloat("walk_up", change.y);

    }

    void FixedUpdate()
    {
        rb2d.MovePosition(rb2d.position + change * moveSpeed * Time.fixedDeltaTime);

        if(Input.GetKey("left shift"))
        {
            moveSpeed = 150;
            animator.speed = 1.5f;
        }
        else
        {
            moveSpeed = 70;
            animator.speed = 1f;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类密钥控制:单行为
{
私人浮动速度;
私有刚体2d rb2d;
私人矢量2变化;
私人动画师;
布尔正在移动;
布尔在动;
void Start()
{
rb2d=GetComponent();
animator=GetComponent();
}
无效更新()
{
change.x=Input.GetAxisRaw(“水平”);
change.y=Input.GetAxisRaw(“垂直”);
如果(数学绝对值(变化x)>数学绝对值(变化y))
{
改变y=0;
}
其他的
{
变化x=0;
}
SetFloat(“向右行走”,change.x);
SetFloat(“walk_left”,-change.x);
SetFloat(“向下行走”,-change.y);
设置float(“向上走”,change.y);
}
void FixedUpdate()
{
rb2d.MovePosition(rb2d.position+change*moveSpeed*Time.fixedDelatime);
if(Input.GetKey(“左移”))
{
移动速度=150;
animator.speed=1.5f;
}
其他的
{
移动速度=70;
animator.speed=1f;
}
}
}

非常感谢您的帮助

我想问题在于您的移动速度。你可以用
时间乘以你的速度。FixedDelatime
但这不是必需的,因为这个值是常量。相反,只需将速度设置为1,然后删除
Time.fixedDeltaTime


注意:
Time.deltaTime
用于使角色每帧移动一定量,因为帧之间的时间越短,角色移动越少<代码>时间。FixedDelatime保持不变,因为它是每个物理帧之间的时间。

如果我理解正确,它保持移动的原因是因为输入保持大于零。您可以添加一个变量来检查按键是否已按下以停止移动

我认为这里有两种选择

  • 让用户每次想要移动时都单击(按键、抬起手指、按键等)
  • 添加一个计时器,使其每N秒移动一次

  • 对于(1)我们可以添加一个变量,表示我们已经在移动了。

    //Changes IsXMoving and IsYMoving to a single boolean
    bool isMoving = false;
    
    ///Start, Update, etc
    
    void FixedUpdate()
    {
        //Check if we are already moving - if we are not, update movement
        if (!isMoving)
        {
            rb2d.MovePosition(rb2d.position + change * moveSpeed * Time.fixedDeltaTime);
    
            if(Input.GetKey("left shift"))
            {
                moveSpeed = 150;
                animator.speed = 1.5f;
            }
            else
            {
                moveSpeed = 70;
                animator.speed = 1f;
            }
        }
        
        //Check we are moving by getting the magnitude - if it zero, we are still
        isMoving = (change.magnitude != 0);
    }
    
    //Removed IsXMoving and IsYMoving
    //lastMove will store the time we last allowed movement and delay is 
    //the minimum time between movements in seconds
    float lastMove;
    float delay = 1; //1 second
    
    
    
    void FixedUpdate()
    {
        //Check if enough time has passed to be allowed to move
        if (lastMove + delay < Time.time)
        {
            rb2d.MovePosition(rb2d.position + change * moveSpeed * Time.fixedDeltaTime);
    
            if(Input.GetKey("left shift"))
            {
                moveSpeed = 150;
                animator.speed = 1.5f;
            }
            else
            {
                moveSpeed = 70;
                animator.speed = 1f;
            }
            
            //Only update the time if we are moving
            if (change.magnitude > 0)
                lastMove = Time.time;
        }
    }
    
    第二个选项(2)基本上会用一个基本计时器来代替它。

    //Changes IsXMoving and IsYMoving to a single boolean
    bool isMoving = false;
    
    ///Start, Update, etc
    
    void FixedUpdate()
    {
        //Check if we are already moving - if we are not, update movement
        if (!isMoving)
        {
            rb2d.MovePosition(rb2d.position + change * moveSpeed * Time.fixedDeltaTime);
    
            if(Input.GetKey("left shift"))
            {
                moveSpeed = 150;
                animator.speed = 1.5f;
            }
            else
            {
                moveSpeed = 70;
                animator.speed = 1f;
            }
        }
        
        //Check we are moving by getting the magnitude - if it zero, we are still
        isMoving = (change.magnitude != 0);
    }
    
    //Removed IsXMoving and IsYMoving
    //lastMove will store the time we last allowed movement and delay is 
    //the minimum time between movements in seconds
    float lastMove;
    float delay = 1; //1 second
    
    
    
    void FixedUpdate()
    {
        //Check if enough time has passed to be allowed to move
        if (lastMove + delay < Time.time)
        {
            rb2d.MovePosition(rb2d.position + change * moveSpeed * Time.fixedDeltaTime);
    
            if(Input.GetKey("left shift"))
            {
                moveSpeed = 150;
                animator.speed = 1.5f;
            }
            else
            {
                moveSpeed = 70;
                animator.speed = 1f;
            }
            
            //Only update the time if we are moving
            if (change.magnitude > 0)
                lastMove = Time.time;
        }
    }
    
    //删除了IsXMoving和IsYMoving
    //lastMove将存储上次允许移动的时间,延迟为
    //移动之间的最短时间(以秒为单位)
    浮动最后移动;
    浮动延迟=1//1秒
    void FixedUpdate()
    {
    //检查是否有足够的时间允许移动
    if(最后移动+延迟<时间)
    {
    rb2d.MovePosition(rb2d.position+change*moveSpeed*Time.fixedDelatime);
    if(Input.GetKey(“左移”))
    {
    移动速度=150;
    animator.speed=1.5f;
    }
    其他的
    {
    移动速度=70;
    animator.speed=1f;
    }
    //只有在我们移动时才更新时间
    如果(变化幅度>0)
    lastMove=Time.Time;
    }
    }