C# Movement Script (Basic)

I just recently changed from Javascript to C#, as many people were telling me that is the better way to go. The problem is, that I need to make a basic movement script, and I need someone kind enough to show me how, give me a link to a tutorial, or even just give me a basic script and a brief discription of what certain parts are.

Thanks alot.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    //Variables
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F; 
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;

    void Update() {
        CharacterController controller = GetComponent<CharacterController>();
        // is the controller on the ground?
        if (controller.isGrounded) {
            //Feed moveDirection with input.
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            //Multiply it by speed.
            moveDirection *= speed;
            //Jumping
            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;
            
        }
        //Applying gravity to the controller
        moveDirection.y -= gravity * Time.deltaTime;
        //Making the character move
        controller.Move(moveDirection * Time.deltaTime);
    }
}

This is a script taken from the Unity script Reference found here.

If you are used to JavaScript, you should be able to figure out the main parts.

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
transform.Translate(Vector3.forward * MoveUp * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
transform.Translate(Vector3.back * MoveDown * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
transform.Translate(Vector3.right * MoveLeft * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A)){
transform.Translate(Vector3.left * MoveRight * Time.deltaTime);
}
}

For Quake style controls … Obviously, it’s missing a fire button and the instantiate bullet procedure, but that’s a whole other kettle of fish!

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

public class MoveMe : MonoBehaviour {
	public float speed = 6.0F;
	public float jumpSpeed = 8.0F; 
	public float gravity = 20.0F;
	private Vector3 moveDirection = Vector3.zero;
	private float turner;
	private float looker;
	public float sensitivity;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		CharacterController controller = GetComponent<CharacterController>();
		// is the controller on the ground?
		if (controller.isGrounded) {
			//Feed moveDirection with input.
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			//Multiply it by speed.
			moveDirection *= speed;
			//Jumping
			if (Input.GetButton("Jump"))
				moveDirection.y = jumpSpeed;

		}
		turner = Input.GetAxis ("Mouse X")* sensitivity;
		looker = -Input.GetAxis ("Mouse Y")* sensitivity;
		if(turner != 0){
			//Code for action on mouse moving right
			transform.eulerAngles += new Vector3 (0,turner,0);
		}
		if(looker != 0){
			//Code for action on mouse moving right
			transform.eulerAngles += new Vector3 (looker,0,0);
		}
		//Applying gravity to the controller
		moveDirection.y -= gravity * Time.deltaTime;
		//Making the character move
		controller.Move(moveDirection * Time.deltaTime);
	}
}

@zedsmith52 I copied the scripts you did but it won’t work do you know what’s going on?

Welcome to the world of C#! It’s a great choice for Unity development. I recently created a video tutorial that walks through the process of creating a basic movement script in Unity. You can check it out here: Creating a Basic Movement Script in Unity | Step-by-Step Tutorial.