Question Player Movement script in Unity not working

AlexShad

New member
Joined
Aug 14, 2019
Messages
1
Programming Experience
1-3
I am creating a Player Movement script in Unity C# and it is not working. Please review my code and tell me the problem.
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    // Define stuff
    public Rigidbody2d rb;

    private Movement = Input.GetAxis("Horizontal");
    private float Jump = Input.Button("Up");
    private Vector2 Jump = new Vector2(0,);
    public float speed = 10f;

    // Physics
    void FixedUpdate()
    {
        rb.AddForce(Movement * speed);
        rb.AddForce(Jump * speed);
    }
}
 
Last edited by a moderator:
In the future, please put your code in CODE tags. It will make it much easier for other people coming to the forum to read.

Anyway, can you describe "not working". Is the script failing to load or compile? Is the script successfully being loaded and run, but you are not seeing the results you were expecting? If so what was your expected result and result are you actually getting?

As an aside, this line looks wrong due to the comma just before the closing parenthesis. Looks like a syntax error:
C#:
private Vector2 Jump = new Vector2(0,);

And these look wrong if you plan on actively getting the input values:
C#:
private Movement = Input.GetAxis("Horizontal");
private float Jump = Input.Button("Up");
because those variables will only be initalized when the class in instantiated. They won't be updated later while your game is running and the user is jamming on the controller.
 
For future reference, the title should be a brief summary of the problem and the post should contain a FULL and CLEAR explanation.
Please review my code and tell me the problem.
is part of a post, not a title. Such an explanation must also include a description of exactly what you expect and exactly how what you actually see differs from that.
 

Latest posts

Back
Top Bottom