Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[HELP] jump controller

Discussion in 'Scripting' started by Harper_aimee, Sep 19, 2019.

  1. Harper_aimee

    Harper_aimee

    Joined:
    Aug 25, 2018
    Posts:
    10
    Hello,

    I have the below script I think it is correct but the animation only plays and there is no height, am I missing something?

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

    public class Character_Controller : MonoBehaviour
    {
    float speed = 5;
    float rotSpeed = 80;
    float rot = 0f;
    float gravity = 8;
    float jumpPower = 8;


    Vector3 moveDir = Vector3.zero;

    CharacterController controller;
    Animator anim;

    void Start()
    {
    controller = GetComponent<CharacterController>();
    anim = GetComponent<Animator>();
    }

    void Update()
    {
    if (controller.isGrounded)
    {
    if (Input.GetKey(KeyCode.W))
    {
    anim.SetInteger("condition", 1);
    moveDir = new Vector3(0, 0, 1);
    moveDir *= speed;
    moveDir = transform.TransformDirection(moveDir);
    }
    if (Input.GetKeyUp(KeyCode.W))
    {
    anim.SetInteger("condition", 0);
    moveDir = new Vector3(0, 0, 0);
    }
    if (Input.GetKeyDown(KeyCode.Space))
    {
    GetComponent<Rigidbody>().AddForce(new Vector3(0f, jumpPower, 0f));
    }
    }
    rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
    transform.eulerAngles = new Vector3(0, rot, 0);

    moveDir.y -= gravity * Time.deltaTime;
    controller.Move(moveDir * Time.deltaTime);
    }
    }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    My first guess would be that it's the way you jump. AddForce adds a force, but jumping generally makes use of ForceMode.Impulse, since all the force should be applied in an instant. So it may be that you just dont jump any noticable height, because gravity directly cancels out your jump force.

    Also, you should not use GetComponent in the Update() loop, since it's pretty expensive. Rather do it once in Start() and assign the rigidbody to a class variable, which you can then reference when you want to apply a force.

    Last but not least, please use code tags to make the code more readable. It's the <> button in the textfield. It's also the first sticky on this sub-forum if you want to see some examples. Reading unformatted plain-text code is pretty exhausting.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Please post code using CODE tags so it is more easily readable. Did you mean to use Vector3(0,0,1) for your jump? I would think you'd want Vector3(0,1,0).
     
  4. Harper_aimee

    Harper_aimee

    Joined:
    Aug 25, 2018
    Posts:
    10
    Hello,

    See attached script, thank you for your comments so far, hopefully I can sort this issue with your help.

    I am using a bool is jumping true and false on my animation controller coming from idle to jump and from jump to idle.

    Thank you again.

    Aimee
     

    Attached Files:

  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Did you try applying the force for your jump using ForceMode.Impulse as i suggested?
     
  6. Harper_aimee

    Harper_aimee

    Joined:
    Aug 25, 2018
    Posts:
    10
    How would i do that ?
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590