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 AddForce seems to save the velocity and continue it.

Discussion in 'Physics' started by QuackedUp, May 15, 2021.

  1. QuackedUp

    QuackedUp

    Joined:
    May 3, 2021
    Posts:
    10
    Hello! I have a problem with rigidbody.AddForce. I have a character movescript and the player looks at the cursor, so on pressing the "Jump" key it should add force in the direction where the player looks. And it only kinda works. link to gif : https://imgur.com/a/zfUHwGr
    code :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float movementSpeed = 5f;
    8.     public float rollforce = 15f;
    9.     public Rigidbody2D rb;
    10.     public Camera cam;
    11.     Vector2 movement;
    12.     Vector2 mousePos;
    13.     private bool isrolling;
    14.     void Update()
    15.     {
    16.  
    17.  
    18.         if (Input.GetButtonDown("Jump"))
    19.         {
    20.             isrolling = true;
    21.         }
    22.         if (Input.GetButtonUp("Jump"))
    23.         {
    24.             isrolling = false;
    25.         }
    26.  
    27.         movement.x = Input.GetAxisRaw("Horizontal");
    28.         movement.y = Input.GetAxisRaw("Vertical");
    29.  
    30.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    31.     }
    32.  
    33.     private void FixedUpdate()
    34.     {
    35.         if (isrolling == true)
    36.         {
    37.             Debug.Log("pressed");
    38.             //rb.AddForce(transform.forward * rollforce, ForceMode2D.Impulse);
    39.             rb.AddRelativeForce(transform.up * rollforce, ForceMode2D.Force);
    40.         }
    41.         if (isrolling == false)
    42.         {
    43.             rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime);
    44.  
    45.             Vector2 lookDir = mousePos - rb.position;
    46.             float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
    47.             rb.rotation = angle;
    48.         }
    49.  
    50.  
    51.     }
    52. }
    53.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,589
    You're using two completely different methods for movement here. You're adding a force which then, during the simulation step, updates the velocity which cause it to move. At this point the body has velocity.

    You then (when rolling) start issuing kinematic movements, ignoring the velocity the body has, telling it to move to specific positions but you leave any velocity it has intact so I'm not sure why you are surprised it still has velocity. Maybe you expect the MovePosition to modify the velocity. It has no reason to do that; it's totally under your control.

    If when you start rolling, you want to zero the velocity the object has then do so by setting the following property to zero: Rigidbody2D.velocity either before the MovePosition or more appropriately when you set Isrolling to true.
     
    QuackedUp likes this.
  3. QuackedUp

    QuackedUp

    Joined:
    May 3, 2021
    Posts:
    10
    Thanks for the help! I'm quite new to unity thats why i asked.