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 fucked 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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
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.
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.
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.
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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if(!Input.GetKey("up") && !Input.GetKey("down"))
{
movement.y = 0.0f;
}
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
Debug.Log(movement);
}
}
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?