Hi
I am using First Person Control, I disabled MouseLook, so I can only control the game by keyboard arrows.But the arrows can only go forward/backward/right/left, I cannot turn around. I want when I press the right arrow, I will turn 45 degrees or 90 degrees, then I can use the up arrow to move forward. How can I do this?
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controls your movement on the x-axis/sideways (horizontal input keys) and z-axis/in&out (vertical input keys).
If you remove 'Input.GetAxis("Horizontal")' you'll no longer be able to strafe. You can add a line right after it to rotate you along the y-axis based on the horizontal input keys:
I have been reading your post an the indications from Scribe and finally got it work!
As I couldn’t attach the file, below you’ll find the complete code.
Regards,
German
private var motor : CharacterMotor;
// Use this for initialization
function Awake () {
motor = GetComponent(CharacterMotor);
}
// Update is called once per frame
function Update () {
// Get the input vector from kayboard or analog stick
//var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); Línea Original
var directionVector = new Vector3(0, Input.GetAxis("Vertical"));
transform.Rotate(Vector3.up, Input.GetAxis("Horizontal") * 0 * Time.deltaTime); //Linea Adicional
if (Input.GetKey(KeyCode.DownArrow)) transform.Translate(0, 0, -1* Time.deltaTime);//Lineas para otro script?
if (Input.GetKey(KeyCode.UpArrow)) transform.Translate(0, 0, 1* Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow)) transform.Rotate(0, 1, 0);
if (Input.GetKey(KeyCode.LeftArrow)) transform.Rotate(0, -1, 0);//Termina la Modificación
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
I leave here prepared to change the speed of movement and rotation in C#
public class FPSInputController : MonoBehaviour
{
private CharacterMotor motor;
public float rotationSpeed = 20.0f;
public float speed = 10.0f;
void Awake()
{
motor = GetComponent<CharacterMotor>();
}
void Update()
{
Vector3 directionVector = new Vector3(0, Input.GetAxis("Vertical"));
transform.Rotate(Vector3.up, Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)){
transform.Translate(0, 0, -1* speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) {
transform.Translate (0, 0, 1 * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow)|| Input.GetKey(KeyCode.D)) {
transform.Rotate(0, 1, 0);
}
if (Input.GetKey (KeyCode.LeftArrow)|| Input.GetKey(KeyCode.A)) {
transform.Rotate(0, -1, 0);
}
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
float directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}