Question by
Kirbins9 · Oct 12, 2020 at 09:43 AM ·
movementphysicscharactercontrollergravity3rd person controller
Character only falls when button is pushed down
Hello, I am very new to game development. I'm making a 2.5 D style game using a 3rdperson character-controller. When I applied gravity my character only falls as long as a button is being pushed. Here is my script:
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Bug on Character movement 0 Answers
Should I use Rigidbodies in a top down 2D game if I don't need physics? 0 Answers
Move Player With Moving Vehicle 1 Answer
Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers
Character not jumping smoothly using CharacterController 0 Answers