Character cannot move

Marsnoob

New member
Joined
Nov 17, 2022
Messages
3
Programming Experience
Beginner
Good morning, masters.

I faced a problem when I tried to create and control a player charater. I followed the tutorial online and the console detected my input, but the character did not move. How do I solve it?

1668676014735.png
1668676021209.png
 
Please post your code as text in code tags, not as a screenshot. It is very difficult to read code presented that way on small devices, and adds an extra level of work that needs to be done if we want to try to reproduce the problem ourselves, or to highlight something about the code.

Anyway, from what I can barely read, it looks like you updated the velocity, but did not apply the velocity to compute a new position.
 
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public float speed;
    Rigidbody rb;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // storing player's input
        float input = Input.GetAxisRaw("Horizontal");
        print(input);

        //moving player
        rb.velocity = new Vector2(input * speed, rb.velocity.y);

    }
}
 
Last edited by a moderator:
Please post your code as text in code tags, not as a screenshot. It is very difficult to read code presented that way on small devices, and adds an extra level of work that needs to be done if we want to try to reproduce the problem ourselves, or to highlight something about the code.

Anyway, from what I can barely read, it looks like you updated the velocity, but did not apply the velocity to compute a new position.
sorry, it's my first time posting in the forum.
 
Yup, based on that readable code, it looks like you update the velocity, but never update the actual position.
 

Latest posts

Back
Top Bottom