碰撞后立即停止刚体的移动/旋转 [英] Stop Rigidbody Movement/Rotation instantly after collision

查看:505
本文介绍了碰撞后立即停止刚体的移动/旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望球体从一个位置跳到另一个位置,但不希望它以后平移.我不知道该怎么做.这是我的代码:

I want my sphere to jump from one position to another but don't want it to translate afterward. I can't figure out how to do that. Here's my code:

void Update()
{
    if (!thrown && ((Input.touchCount > 0 
    && Input.GetTouch(0).phase == TouchPhase.Ended) 
    || Input.GetMouseButtonDown(0)))
    {
        rb.isKinematic = false;
        rb.AddForce(new Vector3(0.0f, 15.0f, 5.0f) );
        thrown = true;
    }
}

推荐答案

有很多方法可以使对象在碰撞后立即停止.我会给你两种方法:

There are many ways to make Object stop immediately after collison. I will give you two ways:

方法1 :

在检测到碰撞时将Rigidbody速度设置为0.

Set the velocity of the Rigidbody to 0 when you detect a collison.

如果对象也在旋转,请将angularVelocity设置为0.

If the object is also rotating, set the angularVelocity to 0 too.

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        Rigidbody rbdy = collision.gameObject.GetComponent<Rigidbody>();

        //Stop Moving/Translating
        rbdy.velocity = Vector3.zero;

        //Stop rotating
        rbdy.angularVelocity = Vector3.zero;
    }
}

方法2 :

使用物理材料来控制碰撞时的摩擦量.

Use Physic Material to control the amount of friction during collison.

转到资产> 创建> 物理材料

Bounciness 更改为0.

动态静态摩擦力更改为等于或大于1的值.

Change the Dynamic and Static Frictions to values equals or more than 1.

然后将其连接到Collider上的Material插槽.

Then attach it to the Material slot on your Collider.

这篇关于碰撞后立即停止刚体的移动/旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆