1
\$\begingroup\$

I have a character controller on my character's gameobject and I'm trying to make the character jump. This is working but sometimes he jumps way higher than other times. Does anyone know why this is happening? Here's the code:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
CharacterController cc;
public float Speed;
float Forward;
float Sideways;
float vertical;
public float gravity;
public float Jump;

// Start is called before the first frame update
void Start()
{
    cc = GetComponent<CharacterController>();
}

// Update is called once per frame
void Update()
{
    Forward = Input.GetAxis("Vertical") * Speed;
    Sideways = Input.GetAxis("Horizontal") * Speed;

    if (!cc.isGrounded)
    {
        vertical -= gravity * Time.deltaTime;
    }
    else 
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            vertical = Jump * Time.deltaTime;
        }

        vertical = -8 * Time.deltaTime;
    }

   Vector3 Delta = transform.TransformDirection(Sideways, vertical, Forward);

    cc.Move(Delta * Time.deltaTime);
}
}
\$\endgroup\$
  • \$\begingroup\$ You've scaled the launch velocity by the duration of the current frame — was this deliberate? \$\endgroup\$ – DMGregory May 14 at 1:13
  • \$\begingroup\$ Yes, my character was teleporting otherwise. I've changed the code however and it's working now. Should I post it as the answer? \$\endgroup\$ – JeffBrin May 14 at 1:38
  • 1
    \$\begingroup\$ Yes, working solutions should be posted as answers. \$\endgroup\$ – DMGregory May 14 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.