I have a problem.
When I press A or D the player moves to the right or to the left instantly.
But I would like this to happen more slowly and smoothly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour {
private CharacterController controller;
private Vector3 moveVector;
public float smooth = 2;
private float speed = 5.0f;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float jumpForce = 5.0f;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space)) {
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity* Time.deltaTime;
}
//X
if (Input.GetKeyDown(KeyCode.A))
{
Vector3 newPosition = controller.transform.position;
newPosition.x--;
controller.transform.position = newPosition;
}
if (Input.GetKeyDown(KeyCode.D))
{
Vector3 newPosition = controller.transform.position;
newPosition.x++;
controller.transform.position = newPosition;
}
//Y
moveVector.y = verticalVelocity;
//Z
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
}