Rigidbody2d starts at 0,0 and doesnt move

Hello There
So im kinda new to unity and I need some help,
I have a player when I start it teleports to 0,0 and it cant move its only affected by gravity which works fine, here is my player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControles : MonoBehaviour
{
    public float speed;
    private bool facingRight = true;
    private float moveInput;
    private Rigidbody2D rb;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        var horizontal = new Vector2(Input.GetAxisRaw("Horizontal"), rb.velocity.y);
        transform.position = horizontal * speed * Time.fixedDeltaTime;
        moveInput = Input.GetAxisRaw("Horizontal");
        if (facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (facingRight == true && moveInput < 0)
        {
            Flip();
        }
    }
    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
}

You’re setting the transform’s position instead of the rigidbody velocity. GetAxisRaw will be in a range of -1 to 1, so it will be stuck at the origin on the X axis.

Replace
transform.position
with
rb.velocity

1 Like

Thank you so much!!! :slight_smile:

1 Like