- Home /
Question by
pokerface0958 · Jan 21, 2021 at 08:53 PM ·
movementcharactercontrollergravity
When my player climb something, after a certain point it start to float
I followed a tutorial and made a third person movement using character controller, but for some reason whenever I climb to a specific point, I always start floating and my gravity script doesnt work either. This is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public CharacterController Controller;
public Transform Camera;
public Animator Animator;
public float Speed = 6f;
public float TurnSmoothTime = 0.1f;
float TurnSmoothVelocity;
Vector3 Velocity;
public float Gravity = -19.62f;
public Transform GroundCheck;
public float GroundDistance = 0.4f;
public LayerMask GroundMask;
public bool IsGrounded;
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;
bool IsMoving = false;
bool IsRunning = false;
if (Direction.magnitude >= 0.1f)
{
IsMoving = true;
float TargetAngle = Mathf.Atan2(Direction.x, Direction.z) * Mathf.Rad2Deg + Camera.eulerAngles.y;
float Angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, TargetAngle, ref TurnSmoothVelocity, TurnSmoothTime);
transform.rotation = Quaternion.Euler(0f, Angle, 0f);
Vector3 MoveDirection = Quaternion.Euler(0f, TargetAngle, 0f) * Vector3.forward;
Controller.Move(MoveDirection.normalized * Speed * Time.deltaTime);
if (Input.GetKey(KeyCode.LeftShift))
{
IsRunning = true;
Speed = 10f;
}
}
if(Input.GetKeyDown(KeyCode.Space) && IsGrounded)
{
Velocity.y = Mathf.Sqrt(3f * -2f * Gravity);
}
// Gravity
Velocity.y += Gravity * Time.deltaTime;
Controller.Move(Velocity * Time.deltaTime);
Animator.SetBool("Moving", IsMoving);
Animator.SetBool("Running", IsRunning);
}
}
Any help is appreciated.
Comment
$$anonymous$$aybe use Rigidbody and then you could use something like AddForce / AddConstantForce for gravity
i hoped to use a character controller to add some extra functions along the line. managed to fix the issue as well. thanks tho :)
managed to solve the issue. turns out the collider i used for a 3d object was causing it. this was fixed when i changed it to a mesh collider.