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 - Fatal编程技术网

C# 当雪碧在雪碧上移动时,如何使其变慢?

C# 当雪碧在雪碧上移动时,如何使其变慢?,c#,unity3d,C#,Unity3d,我有一个名为“玩家”的游戏对象,我的地图由短草和长草组成。如果我的球员在长草地上,我希望它慢下来。另外一个问题是,我的游戏中有多个长草游戏对象。以下是它们的屏幕截图: 这就是我当前的移动代码: public class PlayerMovement : MonoBehaviour { public Sprite Up; public Sprite Down; public Sprite Right; public Sprite Left; public

我有一个名为“玩家”的游戏对象,我的地图由短草和长草组成。如果我的球员在长草地上,我希望它慢下来。另外一个问题是,我的游戏中有多个长草游戏对象。以下是它们的屏幕截图:

这就是我当前的移动代码:

public class PlayerMovement : MonoBehaviour
{

    public Sprite Up;
    public Sprite Down;
    public Sprite Right;
    public Sprite Left;
    public float speed;
    private SpriteRenderer sr;


    // Update is called once per frame
    void Update()
    {

        Vector3 move;

        if (Input.GetKey(KeyCode.W))
        {
            GetComponent<SpriteRenderer>().sprite = Up;
            move = new Vector2(0, speed * Time.deltaTime);

            transform.position += move;
        }
        if (Input.GetKey(KeyCode.A))
        {
            GetComponent<SpriteRenderer>().sprite = Left;
            move = new Vector2(speed * Time.deltaTime, 0);

            transform.position -= move;
        }
        if (Input.GetKey(KeyCode.D))
        {
            GetComponent<SpriteRenderer>().sprite = Right;
            move = new Vector2(speed * Time.deltaTime, 0);

            transform.position += move;
        }
        if (Input.GetKey(KeyCode.S))
        {
            GetComponent<SpriteRenderer>().sprite = Down;
            move = new Vector2(0,speed * Time.deltaTime);

            transform.position -= move;
        }
    }
}

// All my code does is that on each WASD, it changes to a different sprite and moves it.
公共类玩家移动:单一行为
{
公共精神;
公众精神崩溃;
公共精神权利;
公共精灵左;
公众浮标速度;
私人喷泉老大;
//每帧调用一次更新
无效更新()
{
矢量3运动;
if(Input.GetKey(KeyCode.W))
{
GetComponent().sprite=Up;
移动=新矢量2(0,速度*时间.deltaTime);
transform.position+=移动;
}
if(Input.GetKey(KeyCode.A))
{
GetComponent().sprite=Left;
移动=新矢量2(速度*时间增量,0);
transform.position-=移动;
}
if(Input.GetKey(KeyCode.D))
{
GetComponent().sprite=Right;
移动=新矢量2(速度*时间增量,0);
transform.position+=移动;
}
if(Input.GetKey(KeyCode.S))
{
GetComponent().sprite=Down;
移动=新矢量2(0,速度*时间.deltaTime);
transform.position-=移动;
}
}
}
//我的代码所做的就是在每一个WASD上,它改变成不同的精灵并移动它。
一个解释将不胜感激,因为我是一个初学者这一点

  • 使用碰撞器,例如BoxCollider,检测玩家是否接触草地
  • 在你的播放器上使用标签
  • 在草地上使用OnCollisionInter2D()方法
  • 使用碰撞器,例如BoxCollider,检测玩家是否接触草地
  • 在你的播放器上使用标签
  • 在草地上使用OnCollisionInter2D()方法

  • 解决这个问题的一个办法是使用

    假设您的玩家附带了某种2D碰撞器,您可以将触发器碰撞器附加到草地对象,然后当玩家通过向PlayerMovement类添加和方法进入/离开触发器时,可以将消息发送到PlayerMovement脚本。下面是一个如何工作的示例:

    void OnTriggerEnter2D(Collider2D col)
    {
        // You can use gameObject.tag to determine what type of object we're colliding with
        if(col.gameObject.tag == "LongGrass"){
            speed = .8f;
        }
    }
    
    void OnTriggerExit2D(Collider2D col)
    {
        if(col.gameObject.tag == "LongGrass"){
            speed = 1f;
        }
    }
    
    我鼓励你调查一下你是否有
    以这种方式检测碰撞时出现问题。您还将确保正确标记草地,以使此方法有效。

    解决此问题的一个方法是使用

    假设您的玩家附带了某种2D碰撞器,您可以将触发器碰撞器附加到草地对象,然后当玩家通过向PlayerMovement类添加和方法进入/离开触发器时,可以将消息发送到PlayerMovement脚本。下面是一个如何工作的示例:

    void OnTriggerEnter2D(Collider2D col)
    {
        // You can use gameObject.tag to determine what type of object we're colliding with
        if(col.gameObject.tag == "LongGrass"){
            speed = .8f;
        }
    }
    
    void OnTriggerExit2D(Collider2D col)
    {
        if(col.gameObject.tag == "LongGrass"){
            speed = 1f;
        }
    }
    
    我鼓励你调查一下你是否有
    以这种方式检测碰撞时出现问题。您还将确保正确标记草地以使此方法起作用。

    仅凭逻辑,如果发现碰撞检测,则减慢精灵的速度?是的,但我不知道如何进行此操作仅凭逻辑,如果发现碰撞检测,则减慢精灵的速度?是的,但我不知道如何进行此操作that@HSN720好像我回答了你的问题,您现在遇到的错误是另一个问题,您应该在此处阅读:如果您确实让代码正常工作,您介意将问题标记为已回答吗?@HSN720似乎是我回答了您的问题,您现在遇到的错误是另一个问题,您应该在此处阅读:如果您确实让代码正常工作,你介意在回答的问题上划线吗?