How do we change the direction of the object without its braking?



  • The subject is moving with the physics of rigidbody.

    How do we sharply change the direction of the object without its braking?

    _agent = GetComponentInChildren<NavMeshAgent>();
    

    _agent.speed = 100;

    //так меняется направление движения:
    _agent.SetDestination(_target.position)



  • If we look at the coordinate system.

    введите сюда описание изображения

    See that when the X axis moves to the right. forward) - the values shall be increased when moving to the left (back.- decrease. The Y axis is similar, but up/low.

    Let's say we have a player moving at a steady speed of 50. It doesn't matter what 50 fuckings a second or a breeze. I mean, every certain time interval, it passes 50: 50-100-150-200... From the logic of the coordinate system, we see that it's moving forward, meaning the values increase. To move back, you need to get down. Two speeds can be used: +50, for back value - 50but it's not very effective. No two values per parameter. We just need to speed with another sign, but the same value (50). How? Of course, multiply by one.

    If he's cleaned up, the character has moved positively.

    rigidbody.AddForce(transform.right * speed);
    

    character heading backwards

    rigidbody.AddForce(transform.right * (speed * -1));
    

    In Unity, for these purposes (if characters are controlled from the keyboard) https://docs.unity3d.com/ScriptReference/Input.GetAxis.html which already returns between -1 and 1, depending on where the laser goes.

    -1 назад, 1 - вперед, 0 - ничего не давит
    

    Example:

    var fwd: float = Input.GetAxis("Vertical");
    rigidbody.AddForce(transform.forward * (driveForce * fwd));
    var steer: float = Input.GetAxis("Horizontal");
    rigidbody.AddForce(transform.forward * (steer * fwd));
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2