Hi guys, I have a little question.
I want to make a third person controller. I wrote the script, made a Cinemachine and tried to play the game. I was able to walk, to sprint but i wasn’t able to jump more than one time in one game. Can anybody help me? Have I maybe a script mistake
Thanks!
PS: Sorry for my bad English
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonController : MonoBehaviour
{
public Camera MyCamera;
public float Speed = 5f;
public float SprintSpeed = 8f;
public float RotationSpeed = 15;
public float JumpSpeed = 5f;
CharacterController MyController;
float mDesiredRotation = 0f;
bool mSprinting = false;
float mSpeedY = 0;
float mGravity = -9.81f;
bool mJumping = false;
// Start is called before the first frame update
void Start()
{
MyController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
if(Input.GetButtonDown("Jump") && !mJumping)
{
mJumping = true;
mSpeedY += JumpSpeed;
}
if(!MyController.isGrounded)
{
mSpeedY += mGravity * Time.deltaTime;
}
else if(mSpeedY < 0)
{
mSpeedY = 0;
}
if(mJumping && mSpeedY < 0)
{
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.down, out hit, .5f, LayerMask.GetMask("Default")))
{
mJumping = false;
}
}
mSprinting = Input.GetKey(KeyCode.LeftShift);
Vector3 movement = new Vector3(x, 0, z).normalized;
Vector3 rotatedMovement = Quaternion.Euler(0, MyCamera.transform.rotation.eulerAngles.y, 0) * movement;
Vector3 verticalMovement = Vector3.up * mSpeedY;
MyController.Move((verticalMovement + (rotatedMovement * (mSprinting ? SprintSpeed : Speed))) * Time.deltaTime);
if (rotatedMovement.magnitude > 0)
{
mDesiredRotation = Mathf.Atan2(rotatedMovement.x, rotatedMovement.z) * Mathf.Rad2Deg;
}
Quaternion currentRotation = transform.rotation;
Quaternion targetRotation = Quaternion.Euler(0, mDesiredRotation, 0);
transform.rotation = Quaternion.Lerp(currentRotation, targetRotation, RotationSpeed * Time.deltaTime);
}
}![alt text][1]