Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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# Unity 2D:RigidBy2D.velocity不';不移动字符_C#_Unity3d - Fatal编程技术网

C# Unity 2D:RigidBy2D.velocity不';不移动字符

C# Unity 2D:RigidBy2D.velocity不';不移动字符,c#,unity3d,C#,Unity3d,我制作了一个2D播放器控制器,它一直工作到我制作了一个跟随摄影机脚本的第二天,现在我的角色无法移动,但动画仍然工作。我尝试使用AddForce()而不是更改Rigidbody2D.velocity,但这也不起作用,两三天后我仍然找不到解决方案。代码如下: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBeh

我制作了一个2D播放器控制器,它一直工作到我制作了一个跟随摄影机脚本的第二天,现在我的角色无法移动,但动画仍然工作。我尝试使用AddForce()而不是更改Rigidbody2D.velocity,但这也不起作用,两三天后我仍然找不到解决方案。代码如下:

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




public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb2d;
    Animator animator;
    SpriteRenderer spriteRenderer;
    float direction;

    public float speed;

    // Start is called before the first frame update
    void Start()
    {
        direction = Input.GetAxisRaw("Horizontal");
        animator = GetComponent<Animator>();
        rb2d=GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -199, 197), 
        transform.position.y, transform.position.z);

        if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && ((Input.GetKey("right") || Input.GetKey("d")) || Input.GetKey("left") || Input.GetKey("a")))
        {
            speed = 20f;
            run();
        }
        else if (!(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && (Input.GetKey("right") || Input.GetKey("d")))
        {
            speed = 10f;
            walk();
        }
        else if (!(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && (Input.GetKey("left") || Input.GetKey("a")))
        {
            speed = 10f;
            walk();
        }
        else
        {
            speed = 0f;
            animator.Play("Player_idle");
        }
    }  

    void walk()
    {
        if (speed > 0) {
            animator.Play("Player_walk_right");
        }
        else if (speed < 0)
        {
            animator.Play("Player_walk_left");
        }
        //rb2d.AddForce(new Vector2(speed * direction * Time.deltaTime, 0));
        rb2d.velocity = new Vector2(speed * direction * Time.deltaTime, rb2d.velocity.y);
        Debug.Log(rb2d.velocity);
    }

    void run()
    { 
        if (speed > 0)
        {
            animator.Play("Player_run_right");
        }

        else if (speed < 0)
        {
            animator.Play("Player_run_left");
        }

        //rb2d.AddForce(new Vector2(speed * direction * Time.deltaTime, 0));
        rb2d.velocity = new Vector2(speed * direction * Time.deltaTime, rb2d.velocity.y);
        Debug.Log(rb2d.velocity);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单行为
{
刚体2d rb2d;
动画师;
喷洒器;
浮动方向;
公众浮标速度;
//在第一帧更新之前调用Start
void Start()
{
方向=输入。GetAxisRaw(“水平”);
animator=GetComponent


如果有人能提供帮助,我们将不胜感激。

如果我们看一下您的代码,有问题的语句应该在这里:

rb2d.velocity = new Vector2(speed * direction * Time.deltaTime, rb2d.velocity.y);
这三个变量中的一个必须为零才能导致问题

潜在问题1:
方向
您似乎只在
Start()
中设置了
方向一次:

在场景启动的帧中,您可能没有按任何键/移动任何操纵杆。这意味着
方向
最终为0,并且不再更改

要么把你的线路改成

rb2d.velocity = new Vector2(speed * Time.deltaTime, rb2d.velocity.y);
(不包括
direction
)或将方向分配语句移动到
Update()
方法

潜在问题2:
Time.deltaTime
正如@Daniel在评论中所说的,
Time.deltaTime
语句可能有问题

  • 检查项目设置/其他脚本以确保
    Time.timeScale
    不等于零
  • 如果#1不起作用,您可以删除
    Time.deltaTime
    ,但我同意您的观点,应该在其中声明以保持游戏在不同帧速率下的一致性。请尝试将
    Update()
    更改为;如果您这样做,则需要使用
    Time.fixeddeltime
  • 将来如何调试
    若要在将来调试此问题,请尝试在有问题的行插入断点并将IDE附加到Unity。如果将鼠标悬停在每个变量上,您应该能够看到哪个变量等于零。

    speed*direction*Time.deltaTime
    似乎为0。尝试删除
    Time.deltaTime
    和ch看看发生了什么。由于某种原因,它似乎仍然不起作用。我的主摄像头也会一点一点地自行向上移动,然后向下移动到它开始的位置。当我按下左右键使角色行走时,角色会向相反方向移动半厘米,然后它就不移动了当我将方向分配移动到Update()时,这种情况再次发生。当我删除Time.deltaTime时,将Update()更改为FixedUpdate(),并将Time.deltaTime更改为Time.fixedDeltaTime,它仍然不起作用。如果它移动一点,可能意味着您需要调整Rigidbody2D设置。检查您的阻力,并检查您使用的物理材质的摩擦力。我的线性阻力为0,角度阻力为0.05。材质为无(物理材质2D).对于约束,我启用了冻结旋转,但我不知道这是否与问题有关
    rb2d.velocity = new Vector2(speed * Time.deltaTime, rb2d.velocity.y);