Jump to content

The player moves back after i stop any user input in Unity

adamc03
 Share

 

In the first scene (grass scene), the player works fine in the grass scene, but inside the house, the player after any input the player moves back a bit.

 

The video of what happens -  https://i.imgur.com/aHXwCR7.mp4

 

my player movement script is below.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class playeMovement : MonoBehaviour
{
 
 
 
    //public Animator animator;
 
    public float moveSpeed = 5f;
 
    public Rigidbody2D rb;
    public Animator animator;
 
    Vector2 movement;
 
    public static playeMovement instance;
 
    public string areaTransitionName;
 
    public void Awake()
    {
        if(instance == null)
        {
 
        }
        else
        {
            Destroy(gameObject);
        }
 
        instance = this;
 
        DontDestroyOnLoad(gameObject);
 
    }
 
    public void Update()
    {
 
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");
 
        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);
 
    }
 
    private void FixedUpdate()
    {
 
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
 
    }
 
 
 
 
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 9/24/2022 at 7:33 PM, adamc03 said:
if(instance == null)
        {
 
        }
        else
        {
            Destroy(gameObject);
        }

Don't know how to fix this issue because I am not familiar with unity but i would rewrite this bit of code as

if (instance != null) Destroy(gameObject);

I am also assuming there is a lot of other code acting on the character that would probably be beneficial to troubleshooting

 

edit: also if you are using c#8 or greater you can use pattern matching to make the null check easier to read

if (instance is not null) Destroy(gameObject);

 

Edited by Takumidesh

If your question is answered, mark it so.   |   LTT Movie Club!   |   F@H

Read the docs. If they don't exist, write them.

Desktop: i7-8700K, RTX 2080, 16G 3200Mhz, EndeavourOS(host), win10 (VFIO), Fedora(VFIO)

Server: ryzen 9 5900x, GTX 970, 64G 3200Mhz, Unraid.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×