Characters y position continously increases/decreases whenever I press anything

It seems as though the problem has to do with the
Vector2 playerVelocity = new Vector2 (moveInput.x,player , RigidBody.velocity.y);

The position y on the transform component of the Player increases/decreases exponentially and i am not sure what is causing this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour

{

Vector2 moveInput;
Rigidbody2D playerRigidBody;
[SerializeField] float movementMod;

void Start()
{
    playerRigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{

    Run();

}
void OnMove(InputValue value)
{
    moveInput = value.Get<Vector2>();
    Debug.Log(moveInput);
}

void Run()
{
     Vector2 playerVelocity = new Vector2 (moveInput.x , playerRigidBody.velocity.y);
    playerRigidBody.velocity = playerVelocity*movementMod;
}

}

Hi Kiau,

When exactly does that happen? Always? Even if you do not press any keys?

Hi Nina,

so when i start, the guy falls down as normal, ican move left and right as normal, it seems as though it happens when i get to the edge of a tile. so i shoot downwards through the bottom of the screen (hitting y = -10000 in seconds) if i leave the tile with no tile below, and shoot upwards if i hit a tile directly infront of me.

thanks for your quick reply :smiley:

dont know if this helps…

It seems to smash through any colider aswell…

Thank you for the additional information. :slight_smile:

It’s just a guess but maybe the problem is playerRigidBody.velocity = playerVelocity*movementMod; in combination with new Vector2 (moveInput.x , playerRigidBody.velocity.y);.

Let’s say movementMod is 2f, and playerRigidBody.velocity.y is 1f in frame 0, you have playerRigidBody.velocity.y = 1f * 2f at the end of frame 0.

In frame 1, you have playerRigidBody.velocity.y == 2f. At the end of that frame, you have 2f * 2f = 4f.

And in frame 2, you have 4f; at the end of that frame 4f * 2f = 8f.

Then 16f, 32f, 64f. And so on. After a few frames, you get a “crazy” y-value.

Log playerRigidBody.velocity.y into your console to see if my assumption is correct.


What exactly is the idea behind movementMod?

Hi Nina again,

Thank you for your help.
THe idea behind the movementMOd was to affect the runSpeed (as Rick called it) and you are obviously right. I seem to have put it in the wrong place multiplying the playerVelocity instead of just the playerRigidBody.velocity.x.

Fixed and now it works

Thank you!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms