Как остановить Rigidbody при столкновении в единствеC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Как остановить Rigidbody при столкновении в единстве

Сообщение Anonymous »


Когда мой RigidBody сталкивается с объектом Collison, событие запускается, но объект не всегда останавливается. Это зависит от скорости, но для меня это неприемлемо. Я подозреваю, что это как-то связано с тем, как работает обновление, но я не могу понять этого.
это код

Код: Выделить всё

public class Mover : MonoBehaviour
{
private Rigidbody _rb;
private bool _isColliding;
[SerializeField] private float moveSpeed = 1f;

// Start is called before the first frame update
void Start()
{
_rb = GetComponent();
}

// Update is called once per frame
void Update()
{
MovePlayer();
}

void MovePlayer()
{

float xValue = Input.GetAxis("Horizontal") * moveSpeed;
float zValue = Input.GetAxis("Vertical") * moveSpeed;

Vector3 movement = new Vector3(xValue, 0f, zValue);

_rb.MovePosition(_rb.position + movement * Time.fixedDeltaTime);

}

private void OnCollisionEnter(Collision collision)
{
_isColliding = true;
_rb.velocity = Vector3.zero;
_rb.angularVelocity = Vector3.zero;
}

}
but when debugging _rb.velocity _rb.angularVelocity seem to already be at zero
What i tried was
  • setting walls to static
  • adding new Physic Material as suggested in older threads
  • i have resized wall collison boxes so that they dont overlap
  • I have set collision detection on rigidBody to Continous
  • tried to catch and handle the event on the "wall" side.

Код: Выделить всё

 public class WallHit : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{

if (collision.gameObject.CompareTag("Player"))
{
Rigidbody rbdy = collision.gameObject.GetComponent();

rbdy.velocity = Vector3.zero;

rbdy.angularVelocity = Vector3.zero;
}
}

}
  • tried with _rb.isKinematic = true; OnCollisionEnter, but it only slows it down permanently
  • and also tried to handle it with raycast

Код: Выделить всё

void Update()
{
// Check for player input
if (!IsColliding())
{
MovePlayer();
}
}

bool IsColliding()
{
// Calculate movement vector based on player input
float xValue = Input.GetAxis("Horizontal") * moveSpeed;
float zValue = Input.GetAxis("Vertical") * moveSpeed;
Vector3 movement = new Vector3(xValue, 0f, zValue);

// Perform a collision check using a Raycast
RaycastHit hit;
if (Physics.Raycast(transform.position, movement, out hit, movement.magnitude * Time.deltaTime))
{
// If a collision is detected, return true
return true;
}

// If no collision is detected, return false
return false;
}

void MovePlayer()
{
// Move the player
float xValue = Input.GetAxis("Horizontal") * moveSpeed;
float zValue = Input.GetAxis("Vertical") * moveSpeed;
Vector3 movement = new Vector3(xValue, 0f, zValue);
_rb.MovePosition(_rb.position + movement * Time.fixedDeltaTime);
}
i am runnign out of ideas and i refusing to belive this cannot be handled


Источник: https://stackoverflow.com/questions/781 ... n-in-unity
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»