DEV Community

calteen
calteen

Posted on

character controller unity

gamedev


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public float playerSpeed = 2.5f;
    public float playerSprint = 3.5f;    
    public CharacterController cc;
    public Transform playerCamera;
public float turnCalmTime = 0.1f;
float turnCalmVelocity;
public float gravity = -9.8f;
public float jumpRange = 1f;
public Vector3 velocity;
public Transform surfaceCheck;
bool onSurface;
public float surfaceDistance = 0.4f;
public LayerMask surfaceMask;
public Animator animator;
void playerMove(){
    float horizontal_axis = Input.GetAxisRaw("Horizontal");
    float Vertical_axis = Input.GetAxisRaw("Vertical");
    float Jump_axis = Input.GetAxisRaw("Jump");



    Vector3 direction = new Vector3(horizontal_axis,0f,Vertical_axis).normalized;
if(direction.magnitude >=0.1f){

animator.SetBool("Idle",false);
animator.SetBool("Walk",true);
animator.SetBool("Run",false);
float targetAngle = Mathf.Atan2(direction.x,direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngle,ref turnCalmVelocity,turnCalmTime );
transform.rotation= Quaternion.Euler(0f,angle,0f);

Vector3 moveDirection = Quaternion.Euler(0f,targetAngle,0f)*Vector3.forward;
cc.Move(moveDirection.normalized *playerSpeed* Time.deltaTime);
}else{
 animator.SetBool("Idle",true);
animator.SetBool("Walk",false);
animator.SetBool("Run",false);
}
}








void Jump(){
    if(Input.GetButtonDown("Jump") && cc.isGrounded){
    animator.SetBool("Idle",false);
        animator.SetTrigger("Jump");
        velocity.y = Mathf.Sqrt(jumpRange*-2*gravity);
    }else{
        if(cc.isGrounded){
   animator.ResetTrigger("Jump");
        }


    }
}
void Sprint(){
    if(Input.GetButton("Sprint") &&Input.GetKey(KeyCode.W)&& onSurface){


        Debug.Log("sprint");
    float horizontal_axis = Input.GetAxisRaw("Horizontal");
    float Vertical_axis = Input.GetAxisRaw("Vertical");
    float Jump_axis = Input.GetAxisRaw("Jump");



    Vector3 direction = new Vector3(horizontal_axis,0f,Vertical_axis).normalized;
if(direction.magnitude >=0.1f){
animator.SetBool("Walk",false);
animator.SetBool("Run",true);

float targetAngle = Mathf.Atan2(direction.x,direction.z) * Mathf.Rad2Deg + playerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y,targetAngle,ref turnCalmVelocity,turnCalmTime );
transform.rotation= Quaternion.Euler(0f,angle,0f);

Vector3 moveDirection = Quaternion.Euler(0f,targetAngle,0f)*Vector3.forward;
cc.Move(moveDirection.normalized *playerSprint* Time.deltaTime);
}else{
animator.SetBool("Walk",true);
animator.SetBool("Run",false);  
}

}
}
private void Update() {
onSurface = Physics.CheckSphere(surfaceCheck.position,surfaceDistance,surfaceMask);

 if(onSurface && velocity.y <0){
        velocity.y = -2f;
    }
velocity.y +=gravity *Time.deltaTime;
    cc.Move(velocity*Time.deltaTime);

    playerMove();
        Sprint();
    Jump();
}
}

Enter fullscreen mode Exit fullscreen mode

hello

Top comments (0)