- Home /
Player movement
Hi, I've written this short script but I can only move forward and backwards. (Not left and right) Can someone please help me out? Thanks !
using UnityEngine; using System.Collections;
public class playermovement : MonoBehaviour {
public float moveSpeed;
public float RotationSpeed;
CharacterController cc;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
Vector3 forward = Input.GetAxis ("Vertical") * transform.TransformDirection (Vector3.forward) * moveSpeed;
transform.TransformDirection(new Vector3(0, Input.GetAxis("Horizontal"),0));
cc.Move(forward * Time.deltaTime);
cc.SimpleMove(Physics.gravity);
}
}
You're transfor$$anonymous$$g the direction of your Horizontal Input.GetAxis but you're not doing anything with it, you're not even saving it in a variable...
Answer by Lukas_Schwarz · Feb 09, 2015 at 11:23 AM
I'm currently not at home, can't debugg it and I'm using an old Version of IE. I'll edit it when I'm back. Here you go:
void Update()
{
//Determine axis movement
float vertical = Input.GetAxis("Vertical") * moveSpeed;
float horizontal = Input.GetAxis("Horizontal") * moveSpeed;
//Convert local direction into global
Vector3 translation = this.transform.TransformDirection(horizontal, 0, vertical);
//Translate Movement
cc.Move(translation * Time.deltaTime);
cc.SimpleMove(Physics.gravity);
}
Feel free to ask questions
Edit: Originally posted code didn't work after trying at home, however the now posted does.
Your answer
Follow this Question
Related Questions
Player is Speeding up in collisions 0 Answers
All Unity Engine games' controls not working 1 Answer
glitching player 0 Answers
Player Movement Relative to Camera? 4 Answers