Question by
Pnukso · Apr 24, 2020 at 09:04 AM ·
movementscripting beginnermovement script3rd person controller
3D character sliding movement,3D Character Sliding
Hi! I want to add sliding feature(pressing ctrl button while running makes character slide) but I don't really know how to add it. This is my code so far:
public float moveSpeed, gravityModifier, jumpPower, runSpeed = 12f;
public CharacterController charCon;
private Vector3 moveInput;
public Transform camTrans;
public float mouseSensitivity;
public bool invertX;
public bool invertY;
private bool canJump, canDoubleJump;
public Transform groundCheckPoint;
public LayerMask whatIsGround;
public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//moving *old*
/*moveInput.x = Input.GetAxis("Horizontal")*moveSpeed*Time.deltaTime;
moveInput.z = Input.GetAxis("Vertical")*moveSpeed*Time.deltaTime;*/
//store y velocity
float yStore = moveInput.y;
//moving *new*
Vector3 vertMove = transform.forward * Input.GetAxis("Vertical");
Vector3 horiMove = transform.right * Input.GetAxis("Horizontal");
moveInput = horiMove + vertMove;
moveInput.Normalize();
if (Input.GetKey(KeyCode.LeftShift))
{
moveInput = moveInput * runSpeed;
}else
{
moveInput = moveInput * moveSpeed;
}
moveInput.y = yStore;
moveInput.y += Physics.gravity.y*gravityModifier * Time.deltaTime;
if(charCon.isGrounded)
{
moveInput.y = Physics.gravity.y * gravityModifier * Time.deltaTime;
}
canJump = Physics.OverlapSphere(groundCheckPoint.position, .25f, whatIsGround).Length > 0;
//Handle Jumping
if(Input.GetKeyDown(KeyCode.Space) && canJump)
{
moveInput.y = jumpPower;
canDoubleJump = true;
} else if (canDoubleJump && Input.GetKeyDown(KeyCode.Space))
{
moveInput.y = jumpPower;
canDoubleJump = false;
}
charCon.Move(moveInput * Time.deltaTime);
//control camera rotation
Vector2 mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;
if(invertX)
{
mouseInput.x = -mouseInput.x;
}
if(invertY)
{
mouseInput.y = -mouseInput.y;
}
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + mouseInput.x, transform.rotation.eulerAngles.z);
camTrans.rotation = Quaternion.Euler(camTrans.rotation.eulerAngles + new Vector3(-mouseInput.y, 0f, 0f));
anim.SetFloat("moveSpeed", moveInput.magnitude);
anim.SetBool("onGround", canJump);
}
Comment
Answer by Catherine200 · Jan 21, 2021 at 03:47 AM
thank you so much @Pnusko for this incredible script