Pergunta

Estou tendo problemas com a física na unidade 3d.Estou querendo que minha bola quique nas paredes e siga em outra direção.Quando a bola atinge a parede, ela simplesmente salta para trás.Tentei mudar a direção para ser ortogonal à direção em que atinge a parede, mas não muda de direção.Devido a isso, a bola continua batendo na parede e quicando para trás.

Em segundo lugar, às vezes a bola atravessa a parede.As paredes possuem colisores de caixa, enquanto a bola possui um colisor de esfera.Todos eles têm dinâmica contínua como modo de detecção de colisão.

Foi útil?

Solução

Aqui está um link para um tópico semelhante:

http://forum.unity3d.com/threads/22063-i-shot-an-arrow-up-in-the-air...?highlight=shooting+arrow

Pessoalmente, eu codificaria a rotação usando Lookat como Gargarathsunman sugere neste link, mas se você quiser fazê-lo com a física, provavelmente precisará construir o dardo em pelo menos algumas partes, pois os outros sugeremNo link e adicione diferentes valores de arrasto e arrastos angulares para cada parte, talvez densidade também.Se você jogou um dardo em um vácuo, nunca iria pousar apontando porque o arrasto de ar toca uma parte tão importante (todas as coisas caem à mesma taxa, independentemente da massa, obrigado Sir Isaac Newton).É uma simulação difícil para o mecanismo físico ficar certo.

Outras dicas

Talvez tente obter o ponto de colisão entre sua esfera e sua parede, em seguida, capture a velocidade do corpo rígido e reverta-a pelo ponto de colisão normal.

um exemplo de script para fazer isso ---> (coloque este script em uma parede com colisor)

Script C#:

public class WallBumper : MonoBehaviour
{
    private Vector3 _revertDirection;
    public int speedReflectionVector = 2;
    /***********************************************
    * name : OnCollisionEnter
    * return type : void
    * Make every gameObject with a RigidBody bounce againt this platform
    * ********************************************/
    void OnCollisionEnter(Collision e)
    {
        ContactPoint cp = e.contacts[0];
        _revertDirection = Vector3.Reflect(e.rigidbody.velocity, cp.normal * -1);
        e.rigidbody.velocity = (_revertDirection.normalized * speedReflectionVector);
    }
}

I recently has an issue with a rocket going through targets due to speed and even with continuous dynamic collision detection I couldn't keep this from happening a lot.

I solved this using a script "DontGoThroughThings" posted in wiki.unity3d.com. This uses raycasting between current and previous positions and then ensures the frame ends with the colliders connected for messages an OnTrigger event. Has worked everytime since and it's just a matter of attaching the script so super easy to use.

I think the physics answer is as others have suggested to use multiple components with different drag although typically I think you only want a single RigidBody on the parent. Instead of direction using transform.LookAt you could try and calculate using Quaternion.LookRotation from the rigidbody.velocity. Then use Vector3.Angle to find out how much are are off. The greater the angle diference the more force should be experienced and then use RigidBody.ApplyTorque. Maybe use the Sin(angleDifference) * a constant so less force is applied to torque as you approach proper rotation.

Here is some code I used on my rocket although you'll have to substitute some things as I was pointing toward a fixed target and you'll want to use your velocity.

var rotationDirection = Quaternion.LookRotation(lockedTarget.transform.position - this.transform.position);
var anglesToGo = Vector3.Angle(this.transform.rotation.eulerAngles, rotationDirection.eulerAngles);

if (anglesToGo > RotationVelocity)
{
    var rotationDirectionToMake = (rotationDirection * Quaternion.Inverse(this.transform.rotation)).eulerAngles.normalized * RotationVelocity;

    transform.Rotate(rotationDirectionToMake);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top