Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Problems with trying to make an (parabolic) leap using addforce [Solved]

Discussion in 'Physics' started by Misterkuuul, Apr 30, 2024.

  1. Misterkuuul

    Misterkuuul

    Joined:
    Feb 11, 2021
    Posts:
    10
    Hi everybody,

    I recently added a wolf enemy to my game, and I'm trying to add a behavior that would make the wolf leap forward toward its prey, in this case the player. The problem is that the Y-axis never gets changed, and thus the wolf never leaps forward.

    I try to change those axes using two separate AddForce functions, one for the horizontal leap and one for the vertical leap.
    The horizontal function works, but sadly the vertical doesn't work.

    What did I do wrong and how could I fix the vertical jump?

    Screenshot rigidbody settings: https://imgur.com/a/bKDninF

    Ps. I don't suspect that Navmash is the problem, but I do use the Navmesh agent to move this enemy.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LeapAttackBehaviour : MonoBehaviour
    4. {
    5.     [SerializeField] private float jumpHeight = 10f;  // Controls the jump height
    6.     [SerializeField] private float horizontalMultiplier = 3f;  // Controls the jump distance
    7.     [SerializeField] private float allowedJumpDistance = 30f;
    8.     [SerializeField] private float cooldownTime = 2f;
    9.  
    10.     private float cooldown;
    11.     private float maxcooldown = 3f;
    12.  
    13.     private Rigidbody rb;
    14.     private EnemyAwareness enemyAwareness;
    15.  
    16.     private void Awake()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.         enemyAwareness = GetComponent<EnemyAwareness>();
    20.  
    21.         cooldown = maxcooldown;
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (enemyAwareness.GetAggro)
    27.         {
    28.             float playerDistance = enemyAwareness.Distance;
    29.  
    30.             cooldown -= Time.deltaTime;
    31.  
    32.             if (playerDistance < allowedJumpDistance && cooldown <= 0)
    33.             {
    34.                 JumpTowardsPlayer();
    35.                 cooldown = maxcooldown;
    36.  
    37.                 Debug.Log("Jumping method activated");
    38.             }
    39.         }
    40.  
    41.         //Debug.Log(enemyAwareness.GetPlayerTransform.position);
    42.     }
    43.  
    44.     private void JumpTowardsPlayer()
    45.     {
    46.         Debug.Log("Jumping behaviour activated");
    47.  
    48.         //Calculate the direction towards the player
    49.         Vector3 direction = (enemyAwareness.GetPlayerTransform.position - transform.position).normalized;
    50.  
    51.         //Calculate the jump direction (upward)
    52.         Vector3 jumpDirection = Vector3.up * jumpHeight;
    53.  
    54.         //Calculate the leap direction (forward)
    55.         Vector3 leapDirection = direction * horizontalMultiplier;
    56.  
    57.         //Apply forces to make the enemy leap
    58.         rb.AddForce(jumpDirection, ForceMode.Impulse); //The problem!
    59.         rb.AddForce(leapDirection, ForceMode.Impulse);
    60.     }
    61. }
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    You'll need to disable the agent component and then do the jump. When the wolf lands you can re-enable the agent.
     
  3. Misterkuuul

    Misterkuuul

    Joined:
    Feb 11, 2021
    Posts:
    10
    Thank you! This does indeed work. Do you know why the Navmesh makes the enemy "stick" to the ground?