I tried to write this script but the object doesn’t move at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour {
public float speed = 10.0f;
Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
float horizontalMovement = Input.GetAxis("Horizontal") * speed;
float verticalMovement = Input.GetAxis("Vertical") * speed;
horizontalMovement *= Time.deltaTime;
verticalMovement *= Time.deltaTime;
rb.AddForce(new Vector2(horizontalMovement, verticalMovement));
}
}
Everything looks right here, so you’re probably missing something simple.
Debug to make sure you’re getting actual values for the horizontal and vertical movement.
Make sure you didn’t accidentally change the public value of speed to 0.
Additionally it looks like the values you’re adding here might actually be extremely small, thus appearing like you’re not moving at all when in fact you are but very slowly. Try multiplying your addforce amount.
public class movement : MonoBehaviour {
public float speed = 10.0f;
public float thrust = 500;
Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
float horizontalMovement = Input.GetAxis("Horizontal") * speed;
float verticalMovement = Input.GetAxis("Vertical") * speed;
horizontalMovement *= Time.deltaTime;
verticalMovement *= Time.deltaTime;
rb.AddForce(new Vector2(horizontalMovement, verticalMovement) * thrust);
}
}