Problem with my mouvement script

Hello guys!
I’m new on Unity and can’t understand why my script isn’t working. I mean I did a small script to make my character move but he doesn’t. This is the script, there is no compiler errors but it still doesn’t work:

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

public class Controller : MonoBehaviour {
    public float speed = 50f;
            void Fixedupdate (){
            Rigidbody2D rb = GetComponent<Rigidbody2D>();
            float x = Input.GetAxis("Horizontal");
            float y = Input.GetAxis("Vertical");
            rb.AddForce((Vector2.right*speed)*x);
        }
}

Thanks for taking time!

I’m pretty sure I know what the problem is. The way unity runs there is an Update() call and FixedUpdate() calls. The difference is that Unity draws a frame then calls Update. This will vary depending on how fast Unity can draw a frame… it might be 30 Update calls a second (30FPS) or 60.

Now Fixed update occurs at a constant rate. I think the default is 50FPS … . so if you were running at 60 FPS your calls would look like this:

Update()
FixedUpdate()
Update()
FixedUpdate()


Update()
Update()
FixedUpdate()

Anyway all the Input stuff, especially things that check when a key or a mouse button is fixed pressed down is only true during that frame, specifically during that Update call. So i bet its not true during the FixedUpdate. Though you don’t want to AddForce in Update since it might get added multiple times or not often enough.

You could try something like this and see if it works:

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

public class Controller : MonoBehaviour
{
    public float speed = 50f;
    private  bool doForce;
    private float xForce;
    private float yForce;

    void Update()
    {
        if (!doForce)
        {
            if (Input.GetAxis("Horizontal")!=0 || Input.GetAxis("Vertical" !=0)
            {
                xForce = Input.GetAxis("Horizontal");
                yForce = Input.GetAxis("Vertical");
                doForce = true;
            }
        }
    }
    void Fixedupdate()
    {
        Rigidbody2D rb = GetComponent<Rigidbody2D>();
        if (doForce)
            rb.AddForce((Vector2.right * speed) * xForce);
        doForce = false;
    }
}

I don’t use the Unity Physics engine much so I’m not sure if thats the best way to go about it. I usually manually move my objects around like this:

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

public class Controller : MonoBehaviour
{
    // this is in units per second
    public float speed = 5f;

    void Update()
    {
        float horiz = Input.GetAxis("Horizontal");
        float vert = Input.GetAxis("Horizontal");

        Vector3 move = new Vector3(horiz * speed * Time.deltaTime, 0, vert * speed * Time.deltaTime);
        transform.position += move;
    }
}

Thank you a lot for your answer! I didn’t realize the problem was “FixedUpdate”!

pretty much, although the examples I’ve seen tend to go with building a Vector3 in the Update off the input and then using that to apply a direct velocity update in FixedUpdate; although I think it’s down to the kind of movement you’re looking to generate.