Moving the character LEFT and RIGHT relative to its rotation

Hi, so as the title says I want to add left and right movement to my character relative to its rotation. I’ve done this to forwards and backwards, but left and right isn’t as simple.

Code :

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
	public float speed;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey (KeyCode.W)) {
			transform.position += transform.forward * (float)Time.deltaTime * speed;
		}
		if (Input.GetKey (KeyCode.S)) {
			transform.position -= transform.forward * (float)Time.deltaTime * speed;
		}
	}
}

Thats its, quite simple. So once again, I want to add right and left movement relative to the rotation of the player.

Instead of changing transform.position you can try using transform.translate.

if (Input.GetKey (KeyCode.A)) {
transform.Translate(Vector3.left * Time.deltaTime * speed, Space.Self); //LEFT
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate(Vector3.right * Time.deltaTime * speed, Space.Self); //RIGHT
}

Space.Self makes it relative to local rotation. Make the same with forward and backward by using Vector3.Forward and Vector3.Back

Hi !

I hope this answer your question :

public float speed = 1f;
    
    void FixedUpdate () {
    	// 1. Get the Input information
        // You can custom the axis key and sensibility at
        // Edit > Project Settings > Input
    	float h = Input.GetAxis ("Horizontal");
    	float v = Input.GetAxis ("Vertical");
    
    	// 2. Create the move vector
    	// /!\ Input Horizontal axis positive button
        //      need to be the right move button (right arrow, D, ...)
    	Vector3 moveVector = (transform.forward * v) + (transform.right * h);
    	moveVector *= speed * Time.deltaTime;
    
    	// 3. Apply the mouvement to the local position
    	transform.localPosition += moveVector;
    }

With this input you’ll have a smoothed mouvement, if you want it sharp change the axis parameters in Edit > Project Settings > Input

Bye !