How can I get my character to move and strafe relative to the way its pointing?

I originally used transform.Translate to handle movement but since that didn’t account for collision I instead switched over to controller.Move. That does account for collisions but does not account for the direction the character is facing.
using UnityEngine;
using System.Collections;

public class PlayerClass : MonoBehaviour
{
	private float hspeed = 10f;
	private float vspeed = 0F;
	private float gravity = 1F;
	private float test = 0;
	private Vector3 moveDirection = Vector3.zero;
	private float hdirection;
	public bool isaccending=true;
	private float movspeed=0;

	// Use this for initialization
	public float movementSpeed = 10;
    public float turningSpeed = 60;
	void Start(){
		
	
	}
    void Update() {
		CharacterController controller = GetComponent<CharacterController>();
        float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
        transform.Rotate(0, horizontal, 0);
        float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
		float strafe = Input.GetAxis("Strafe")* movementSpeed * Time.deltaTime;
		Vector3 movement= new Vector3(-vertical, 0, strafe);
		
		controller.Move(movement);
		isaccending=controller.isGrounded;
		if(controller.isGrounded){
			vspeed =0;
			
		}else{
			vspeed-=gravity* Time.deltaTime;
		}
		
		controller.Move(new Vector3(0,vspeed,0));
		
		
    }
	
}

I think if you add the following line below line 25, you will get what you want. movement = transform.TransformDirection(movement);

2 Answers

2

hey bro when you use “Vector3()” its basically world space, to get directions you need to use stuff like “transform.forward/up/right” which are normalized directional vectors (normalized vectors are vectors that have a value between 0-1 precalculated) so you can then multiply them nice and easily and get the correct direction and length/speed/…etc, hope this helps :D.

You could use the FPSInputController script with the Character Motor. If not, you could at least learn from it.