- Home /
Problems with 2D movement Script
Hey learning how to game dev with unity and got my hands on a couple books to help. First time in doing something in C# and unity. While getting a movement script for a 2d object on a 2d plane I'm having issues getting it to compile properly. Here is what I've got. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewBehaviourScript : MonoBehaviour { //1 public float movementSpeed = 3.0f;
//2
Vector2 movement = new Vector2();
//3
Rigidbody2D rb2d;
private void Start()
{
//4
rb2d = GetComponent<Rigidbody2D>();
}
private void Update()
{
// Keep this empty for now
}
//5
private void FixedUpdate()
{
//6
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
//7
movement.Normalize();
//8
rb2D.velocity = movement * movementSpeed;
} The Compiler is having issues with the last two lines. Any assistance is much appreciated and thank you in advance you kind souls.
Answer by theawesome1 · Dec 04, 2020 at 09:29 PM
I think you need to have the last 2 lines in the update function . The last 2 lines will not work if they are outside of the function because of how to update function works. read here to know more: https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html