- Home /
Question by
StuartCMarshall · Mar 28, 2020 at 06:28 PM ·
rotationmovementtransformsprinting
Player movement adding sprint, issues with rotation
Hi all,
I am just starting out in Unity and I am trying to create a simple player controller.
I have added basic movement and I want to be able to press space to increase the speed to a faster speed.
This seems to work but if I am pressing forward, space and try to turn left nothing happens. I assume this is something to do with the negative value but don't know how to fix this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float walkSpeed = 5.0f;
public float rotationSpeed = 10.0f;
public float sprintSpeed = 20.0f;
private float verticalInput;
private float horizontalInput;
private float speed = 5.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
verticalInput = Input.GetAxis("Vertical");
horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * Time.deltaTime * speed * verticalInput);
transform.Rotate(Vector3.up * Time.deltaTime * rotationSpeed * horizontalInput);
if (Input.GetKey(KeyCode.Space))
{
speed = sprintSpeed;
}
else
{
speed = walkSpeed;
}
}
}
Comment