Hi, i'm having issues with my controller. I'd like to make a controller like the CubeWorld's one : https://www.youtube.com/watch?v=mBePdtnC_28
Indeed, i dont get how to make a smooth movement. I've tried the Vector3.Slerp but it's to go to one point to an other. In this system i'm using "arrows" and i make my character constantly rotating. If you watch closer the character continues to move slower and slower when the man releases his key.
My code is here :
`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();
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);
}
}
}
`
I dont get too how to let my character move while he is jumping.
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;
If u have any help or improvement thanks you.
↧