Good day,
i have a problem in Unity, and i really hope someone can help me out ;).
So i momently pogramm a Third Person Controller with a characterController component and the movement & animations … al worked fine out. But then at the Jump i got stuck because i knew i can move it on the y Value but i dont realy got it working with the gravity so i searched on YT and found a tutorial that says how you can pogramm the gravity… (When not grounded set it to something like -9.8 …) And then i tried it and it worked all fine out. But when i got to the Jump the problem is that he Jumps but instead of going slowly up it goes so fast that it seams like its teleporting. And i searched on the web but yeah then i ended here with the hope that someone can help me out cause i still haven´t figgerd it out.(I put the Code in the bottom). If you need any more informations … to help please let me know and thanks to all that try to help me ![]()
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Vector3 playerVelocity;
private Vector3 move = new Vector3(0, 0, 0);
private Animator ani;
private CharacterController ch;
public Vector3 moveInput;
private float aniMoveVelocity;
private float MoveVelocity;
private bool isJumping;
[Header("References")]
[SerializeField]private PlayerInput inputcontroller;
[Header("Gravity-Settings")]
[SerializeField]private float groundedGravity = -.05f;
[SerializeField]private float Gravity = -9.8f;
[Header("Jumping-Settings")]
[SerializeField]private float initialJumpVelocity = 3;
[SerializeField]private float maxJumpHeight = 1;
[SerializeField]private float maxJumpTime = 0.5f;
[Header("Movement-Settings")]
[SerializeField]private float movementSpeed = 3;
[SerializeField]private float sprintSpeed = 8.5f;
[SerializeField]private float walkAnimationBlendTime = 1.5f;
[SerializeField]private float sprintAnimationBlendTime = 1.0f;
[SerializeField]private float rotationPerFrame = 1.0f;
void Awake()
{
ch = GetComponent<CharacterController>();
ani = GetComponent<Animator>();
setupJumpVariables();
}
private void FixedUpdate() {
moveInput = inputcontroller.MovementInput;
handleAnimations();
Movement();
handleGravity();
jump();
ch.Move(moveInput * Time.deltaTime);
}
private void Update()
{
}
private void handleAnimations()
{
ani.SetFloat("MoveVelocity", MoveVelocity);
aniMoveVelocity = ani.GetFloat("MoveVelocity");
if(aniMoveVelocity > 0 && inputcontroller.pressMoving == false)
{
MoveVelocity = MoveVelocity + walkAnimationBlendTime * -Time.deltaTime;
}
if(aniMoveVelocity > 0.53 && inputcontroller.pressSprinting == false)
{
MoveVelocity = MoveVelocity + sprintAnimationBlendTime * -Time.deltaTime;
}
if(inputcontroller.pressMoving == true && aniMoveVelocity <= 0.5f)
{
MoveVelocity = MoveVelocity + walkAnimationBlendTime * Time.deltaTime;
}
if(inputcontroller.pressMoving == true && inputcontroller.pressSprinting == true && aniMoveVelocity <= 1f)
{
MoveVelocity = MoveVelocity + sprintAnimationBlendTime * Time.deltaTime;
}
}
private void handleGravity()
{
if(ch.isGrounded)
{
moveInput.y = 0;
}
if(!ch.isGrounded)
{
moveInput.y = Gravity;
}
}
private void setupJumpVariables()
{
float timeToApex = maxJumpHeight / 2;
Gravity = (-2 * maxJumpHeight) / Mathf.Pow(timeToApex, 2);
initialJumpVelocity = (2 * maxJumpHeight) / timeToApex;
}
private void jump()
{
if(inputcontroller.pressJump && ch.isGrounded && !isJumping)
{
print("pressed");
isJumping = true;
moveInput.y = initialJumpVelocity;
}
if(isJumping && !inputcontroller.pressJump && ch.isGrounded)
{
isJumping = false;
}
}
private void Movement()
{
if(inputcontroller.pressMoving && !inputcontroller.pressSprinting)
{
move.x = moveInput.x * movementSpeed;
move.y = moveInput.x * movementSpeed;
}
if(inputcontroller.pressMoving && inputcontroller.pressSprinting)
{
move.x = moveInput.x * sprintSpeed;
move.y = moveInput.x * sprintSpeed;
}
}
}