Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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中与游戏对象碰撞时,我如何使其加速?_C#_Unity3d_Collision - Fatal编程技术网

C# 当我的玩家在Unity中与游戏对象碰撞时,我如何使其加速?

C# 当我的玩家在Unity中与游戏对象碰撞时,我如何使其加速?,c#,unity3d,collision,C#,Unity3d,Collision,我正在联合学校做一个项目。我和我的团队决定进行一场无休止的跑步2D游戏。然而,因为这是我第一次使用C#,我不知道如何使我的玩家在与统一体中的游戏对象碰撞时加速。我只知道发生碰撞时如何破坏玩家的健康。请帮帮我!谢谢大家! 在没有看到您编写的任何代码的情况下给出答案是非常困难的,但是由于它是2D的,并且您已经使用了碰撞伤害,您可能使用了一个OnCollisionCenter(),这非常类似:您测试了是否发生了碰撞(您已经完成了),然后使用rigidbody2d向您的播放器添加了力,大概是这样的: p

我正在联合学校做一个项目。我和我的团队决定进行一场无休止的跑步2D游戏。然而,因为这是我第一次使用C#,我不知道如何使我的玩家在与统一体中的游戏对象碰撞时加速。我只知道发生碰撞时如何破坏玩家的健康。请帮帮我!谢谢大家!

在没有看到您编写的任何代码的情况下给出答案是非常困难的,但是由于它是2D的,并且您已经使用了碰撞伤害,您可能使用了一个
OnCollisionCenter()
,这非常类似:您测试了是否发生了碰撞(您已经完成了),然后使用rigidbody2d向您的播放器添加了力,大概是这样的:

public Rigidbody2D rb;

void OnCollisionEnter2D(Collision2D collision)
{
    rb.AddForce(direction * force, ForceMode2D.Impulse); // the ForceMode2D is 
//                                                  optional, it's just so that
//                                              the velocity change is sudden.
}

这应该是可行的。

如果你有一个存储游戏速度的游戏管理器,你也可以这样做:

private float gameSpeedMultiplier = 0.5f;

void OnCollisionEnter(Collision collision)
{
    if(collision.gameObject.CompareTag("Tag of the collided object")
    {
        GameManager.Instance.gameSpeed += gameSpeedMultiplier;
    }
}
public class PlayerContoler:monobhavior
{
公共静态PlayerContoler实例;
公共交通速度;
公共刚体2d-theRB;
公共安全部队;
私人学校停课;
公共检查站;
公共层码头;
私人跳伞;
私人动画师;
私人喷泉;
公共浮点数击退长度、击退力;
私人浮动击退计数器;
公共浮动基金;
公共部门停止输入;
私人空间
{
实例=此;
}
//在第一帧更新之前调用Start
void Start()
{
anim=GetComponent();
theSR=GetComponent();
}
//每帧调用一次更新
无效更新()
{
如果(!PauseMenu.instance.isPause&!stopInput)
{
if(击退计数器0)
{
theSR.flipX=假;
}
}否则
{
KnockbackCounter-=Time.deltaTime;
如果(!theSR.flipX)
{
theRB.velocity=新矢量2(-KnockbackForce,theRB.velocity.y);
}否则
{
theRB.velocity=新矢量2(击打力,theRB.velocity.y);
}
}
}
动画SetFloat(“移动速度”,Mathf.Abs(theRB.velocity.x));
动画设置工具(“isGrounded”,isGrounded);
}
公共无效回退()
{
敲回计数器=敲回长度;
theRB.velocity=新矢量2(0f,击打反作用力);
动画设置触发(“伤害”);
}
}

为我们提供更多信息。玩家类是什么样子的。基本上给我们一个例子,谢谢。请提供你如何移动你的球员的细节。否则,无法判断如何加速移动。欢迎使用堆栈溢出。请访问,看看有什么和。做一些研究,搜索相关话题等;如果您被卡住了,请发布您的尝试、输入、获得和预期输出的详细信息。2d platfomer代码中,有些代码不是endlessRunner。
public class PlayerContoler : MonoBehaviour
{
    public static PlayerContoler instance;

    public float moveSpeed;
    public Rigidbody2D theRB;
    public float jumpForce;

    private bool isGrounded;
    public Transform GroundCheckPoint;
    public LayerMask whatIsGround;

    private bool canDoubleJump;

    private Animator anim;
    private SpriteRenderer theSR;

    public float KnockbackLength, KnockbackForce;
    private float KnockbackCounter;

    public float bonceForce;

    public bool stopImput;

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if(!PauseMenu.instance.isPause && !stopImput)
        {
            if (KnockbackCounter <= 0)
            {

             theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);

             isGrounded = Physics2D.OverlapCircle(GroundCheckPoint.position, .2f, whatIsGround);

             if (isGrounded)
             {
                 canDoubleJump = true;
             }

             if (Input.GetButtonDown("Jump"))
             {
                 if (isGrounded)
                 {
                    theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
                     AudioManager.instance.PlaySFX(10);
                 }
                 else
                 {
                     if (canDoubleJump)
                     {
                        theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
                        canDoubleJump = false;
                         AudioManager.instance.PlaySFX(10);
                     }
                }
            }

            if (theRB.velocity.x < 0)
            {
                theSR.flipX = true;
            }
            else if (theRB.velocity.x > 0)
            {
                theSR.flipX = false;
            }
        } else
        {
            KnockbackCounter -= Time.deltaTime;
            if(!theSR.flipX)
            {
                theRB.velocity = new Vector2(-KnockbackForce, theRB.velocity.y);
            } else
            {
                theRB.velocity = new Vector2(KnockbackForce, theRB.velocity.y);
            }
        }

     }

        anim.SetFloat("moveSpeed", Mathf.Abs(theRB.velocity.x));
        anim.SetBool("isGrounded", isGrounded);
    }

    public void Knockback()
    {
        KnockbackCounter = KnockbackLength;
        theRB.velocity = new Vector2(0f, KnockbackForce);

        anim.SetTrigger("hurt");
    }
}