- Home /
Question by
Skylermaki · Mar 15, 2014 at 05:33 PM ·
charactercontrollerissuesmooththird-person
CubeWorld's controller
Hi, i'm making a character controller like the CubeWorld's One.
I'm wondering how to make the characters move smoothly like it is in cubeworld. I've made one, but it's not as smooth as this one. When you release your key in cubeworld, you char keeps moving a bit longer after. I dont know how to do this.
I've heard of Vector3.Lerp but it's based on origin and destination. Here is my code, i'm not too far. But my moving system is not worth it.
public class ControllerScript : MonoBehaviour {
public float gravity = 5.0F;
public float jumpspeed = 5.0F;
public float speed = 5.0F;
public float rotateSpeed = 125.0F;
public Transform playerCamera;
private Vector3 moveDirection = Vector3.zero;
private float vertVel = 0.0F;
private float turnToValue;
private int moving;
private Vector3 targetDirection;
private Vector3 wantedDirection;
void Update () {
CharacterController controller = GetComponent<CharacterController>();
Screen.showCursor = false;
inputFunction();
if(Input.GetKey("z") || Input.GetKey("d") || Input.GetKey("q") || Input.GetKey("s")){
moving = 1;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(0,playerCamera.eulerAngles.y+turnToValue,0)), Time.deltaTime * rotateSpeed);
}else if(!controller.isGrounded){
moving = 1;
}else moving = 0;
moveDirection = new Vector3(0,0,moving*speed);
moveDirection = transform.TransformDirection(moveDirection);
//targetDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//moveDirection = Vector3.Slerp(transform.position, targetDirection, 1);
//moveDirection = (moveDirection.normalized * speed);
if (controller.isGrounded) {
if (Input.GetButton ("Jump")) {
vertVel = jumpspeed;
}
}
vertVel -= gravity * Time.deltaTime;
moveDirection.y = vertVel;
controller.Move(moveDirection * Time.deltaTime);
}
void inputFunction(){
//Normal Axes
if(Input.GetKey("z") && !Input.GetKey("d") && !Input.GetKey("q")){
turnToValue=0;
}
if(Input.GetKey("s") && !Input.GetKey("d") && !Input.GetKey("q")){
turnToValue=180;
}
if(Input.GetKey("d") && !Input.GetKey("z") && !Input.GetKey("s")){
turnToValue=90;
}
if(Input.GetKey("q") && !Input.GetKey("z") && !Input.GetKey("s")){
turnToValue=-90;
}
//Diagonal
if(Input.GetKey("z") && Input.GetKey("d")){
turnToValue=45;
}
if(Input.GetKey("z") && Input.GetKey("q")){
turnToValue=-45;
}
if(Input.GetKey("s") && Input.GetKey("d")){
turnToValue=135;
}
if(Input.GetKey("s") && Input.GetKey("q")){
turnToValue=-135;
}
if(Input.GetMouseButton(0)){
transform.eulerAngles = new Vector3(0, playerCamera.eulerAngles.y, 0);
}
}
}
If you have any improvement or idea. Thanks :)
Comment