Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player keeps drifting UP after moving. Can't stop it

Discussion in 'Physics' started by FlaxWench, Jun 28, 2022.

  1. FlaxWench

    FlaxWench

    Joined:
    Jan 2, 2022
    Posts:
    3
    Hey all,

    I'm having trouble getting my player sprite to stop drifting upwards after moving. I've pasted code down below which I'm pretty sure I took right from Brackeys. There's a distinct possibility I've F***ed it up.

    The player comes complete with:
    Rigidbody2D,
    and a Collider.
    Gravity is set to 0.
    Z is frozen.
    I've tried upping the drag both linear and angular and both at the same time and upping the mass and increasing speed and drag. I think one time I got it to kind of work when the gravity was increased, and I toyed with a side scroller as opposed to a top down but that's not what I'm trying to make.

    All that to say for some reason I can't get it to do it again, he keeps drifting up, and I'm having issues with moving forward because of it.

    Hope someone can help. Thank you.

    movement script below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.  
    9.     public Rigidbody2D rb;
    10.  
    11.     Vector2 movement;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         movement.x = Input.GetAxisRaw("Horizontal");
    17.         movement.y = Input.GetAxisRaw("Vertical");
    18.     }
    19.  
    20.     private void FixedUpdate()
    21.     {
    22.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    23.     }
    24. }
    25.  
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,201
    Input.GetAxisRaw may return a close-to but non-zero value for an axis if input comes from an analog stick (keyword: deadzone). Try logging the value to see if that‘s the culprit.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    As above but instead of a forum post or just trying different options at random, try debugging it first by inspecting what you're asking physics to do and ensuring that's correct. Do you know what the contents of "movement" are? If not then that should be where you start.
     
  4. FlaxWench

    FlaxWench

    Joined:
    Jan 2, 2022
    Posts:
    3
    Well, after adding Debug.Log(movement); under rb MovePosition in FixedUpdate, it stopped trending up for some reason and began to follow "gravity" I guess. I had the gravity scale set to 10 on the Rigidbody2D component so I switched it to 0, saved and tried again. Same problem, again weird.

    I tried it a few times, the first time was weird and the log occasionally came back with (0.0,0.0) after moving and then letting go of buttons(arrow keys). When moving left or right the x axis seemed to follow suit of 1.0, -1.0, or 0.0 depending on whether I pressed the right, left, or no keys respectively. The weird one is the y axis which was almost constantly either 1.0, or -1.0 and bouncing around like a crazy pants. I get that I need to zero out the movement when I'm not telling it what to do but I'm not sure how to check if I'm not telling it what to do.
     
  5. FlaxWench

    FlaxWench

    Joined:
    Jan 2, 2022
    Posts:
    3
    Ok so I tried this (code below), and it seems to have gotten rid of the unwanted movement. However, I feel that I'm going to run into problems if I ever want an outside force to push the object around. I'd much rather have a fuller understanding of what's going on with the picture at large and WHY the y axis is bouncing like a baby bunny. Thanks again for the debugging suggestion. Debug is still inside the code here.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.  
    9.     public Rigidbody2D rb;
    10.  
    11.     Vector2 movement;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         movement.x = Input.GetAxisRaw("Horizontal");
    17.         movement.y = Input.GetAxisRaw("Vertical");
    18.  
    19.         if(!Input.GetKey("up") && !Input.GetKey("down"))
    20.         {
    21.             movement.y = 0.0f;
    22.         }
    23.     }
    24.  
    25.  
    26.     private void FixedUpdate()
    27.     {
    28.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    29.         Debug.Log(movement);
    30.     }
    31. }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Only Dynamic bodies can have forces applied to them. If that's the case then why are you using MovePosition constantly? This is typically used for Kinematic motion. Constantly telling a Dynamic body to move to a specific position overrides what gravity or external forces are doing but this should be obvious if you step back and think about it.

    If I tell you to move to a specific spot and you always do then someone else called "Mr Gravity" tells you to move to another spot, where would you go?