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

Question 'm looking to create a jump button using my jump script (please help) :)

Discussion in 'Scripting' started by Monki_Games, Mar 18, 2021.

  1. Monki_Games

    Monki_Games

    Joined:
    Jan 12, 2021
    Posts:
    9
    Hi so I'm looking to create a jump button using my jump script, can someone please tell me what i should add or change :), here is my script:

    [/code]
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class HeroCharacterController : MonoBehaviour
    {
    [SerializeField] private float jumpHeight = 10f;
    [SerializeField] LayerMask groundLayers;
    [SerializeField] private float runSpeed = 8f;
    [SerializeField] private AudioClip jumpSoundEffect;

    public PlayerController player;
    private float gravity = -50f;
    private CharacterController characterController;
    private Animator animator;
    private Vector3 velocity;
    private bool isGrounded;
    private float horizontalInput;
    private bool jumpPressed;
    private float jumpTimer;
    private float jumpGracePeriod = 0.2f;
    public float jumpSpeed = 5;
    public GameObject JumpButtonUI;

    // Start is called before the first frame update
    void Start()
    {
    characterController = GetComponent<CharacterController>();
    animator = GetComponent<Animator>();
    player = FindObjectOfType<PlayerController>();
    }

    // Update is called once per frame
    void Update()
    {
    horizontalInput = 1;

    // Face Forward
    transform.forward = new Vector3(horizontalInput, 0, Mathf.Abs(horizontalInput) - 1);


    // IsGrounded
    isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = 0;
    }
    else
    {
    // Add gravity
    velocity.y += gravity * Time.deltaTime;
    }

    characterController.Move(new Vector3(horizontalInput * runSpeed, 0, 0) * Time.deltaTime);


    // Jumping
    jumpPressed = Input.GetButtonDown("Jump");
    if (jumpPressed) ;
    {
    jumpTimer = Time.time;
    jumpTimer = -1;
    }

    if (isGrounded && (jumpPressed || (jumpTimer > 0 && Time.time < jumpTimer + jumpGracePeriod)))
    {
    velocity.y += Mathf.Sqrt(jumpHeight * -2 * gravity);
    if (jumpSoundEffect != null)
    {
    AudioSource.PlayClipAtPoint(jumpSoundEffect, transform.position, 0.5f);
    }
    }

    // Vertical Velocity
    characterController.Move(velocity * Time.deltaTime);

    // Run Animation
    animator.SetFloat("Speed", horizontalInput);

    // Set Animator IsGrounded
    animator.SetBool("IsGrounded", isGrounded);

    // Set parameter for JumpFall Blend Tree Animation
    animator.SetFloat("VerticalSpeed", velocity.y);
    }
    }
     
  2. Monki_Games

    Monki_Games

    Joined:
    Jan 12, 2021
    Posts:
    9
    Here is screenshots of the code :)
    Code Screenshot 1.png Code Screenshot 2.png
     

    Attached Files: