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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

How to get a game object to jump forward

Discussion in 'Scripting' started by AdrianoRivadeneyra, Jun 12, 2021.

  1. AdrianoRivadeneyra

    AdrianoRivadeneyra

    Joined:
    Jun 12, 2021
    Posts:
    2
    I've been having problems with getting my character to jump Forward. I've got it set to jump in place but I can't seem to get the `JumpFor()` to move forward as it jumps. I've tried `Vector3` but it only works in 1 direction and `transform.forward` doesn't seem to work at all.

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

    public class PlayerMovement : MonoBehaviour
    {
    //VARIABLES
    [SerializeField] private float moveSpeed;
    [SerializeField] private float walkSpeed;
    [SerializeField] private float runSpeed;
    private Vector3 moveDirection;
    private Vector3 velocity;
    [SerializeField] private bool isGrounded;
    [SerializeField] private float groundCheckDistance;
    [SerializeField] private LayerMask groundMask;
    [SerializeField] private float gravity;
    [SerializeField] private float jumpHeight;

    //REFERENCES
    private CharacterController controller;
    private Animator anim;
    private Collider Collider;

    private void Start()
    {
    controller = GetComponent();
    anim = GetComponentInChildren();
    Collider = GetComponent(); ;
    }

    private void Update()
    {
    Move();
    }

    private void Move()
    {
    isGrounded = Physics.CheckBox(Collider.bounds.center, Collider.bounds.extents, transform.rotation, groundMask);

    if (isGrounded && velocity.y < 0)
    velocity.y = -2f;

    float moveZ = Input.GetAxis("Vertical");
    moveDirection = new Vector3(0, 0, moveZ);
    moveDirection = transform.TransformDirection(moveDirection);

    if (isGrounded)
    {

    if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
    {
    Walk();
    }
    else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
    {
    Run();
    }
    else if (moveDirection == Vector3.zero)
    {
    Idle();
    }

    moveDirection *= moveSpeed;

    if (isGrounded && Input.GetKeyDown(KeyCode.Space) && moveDirection == Vector3.zero )
    {
    Jump();
    }

    if (isGrounded && Input.GetKeyDown(KeyCode.Space) && Input.GetKey(KeyCode.W) && moveDirection != Vector3.zero)
    {
    JumpFor();
    }

    anim.SetBool("Jump", false);
    anim.SetBool("JumpUp", false);
    }

    if (!isGrounded)
    anim.SetBool("Jump", true);

    controller.Move(moveDirection * Time.deltaTime);

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
    }

    private void Idle()
    {
    anim.SetFloat("Speed", 0, 0.02f, Time.deltaTime);
    }

    private void Walk()
    {
    moveSpeed = walkSpeed;
    anim.SetFloat("Speed", 0.6f, 0.1f, Time.deltaTime);
    }

    private void Run()
    {
    moveSpeed = runSpeed;

    anim.SetFloat("Speed", 1, 0.1f, Time.deltaTime);
    }

    private void Jump()
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    private void JumpFor()
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,725
  3. AdrianoRivadeneyra

    AdrianoRivadeneyra

    Joined:
    Jun 12, 2021
    Posts:
    2
    I Solved it. I ended up using the velocity vector in JumpFor() and gave it a new Vector3 Then I put velocity= transform.TransformDirection(velocity) so it uses the local axis instead of the global one. After that is et it so velocity = Vector3.zero when the player grounds.
    > Blockquote
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    //VARIABLES
    [SerializeField] private float moveSpeed;
    [SerializeField] private float walkSpeed;
    [SerializeField] private float runSpeed;
    [SerializeField] private float JumpDist;

    private Vector3 moveDirection;
    private Vector3 velocity;

    [SerializeField] private bool isGrounded;
    [SerializeField] private float groundCheckDistance;
    [SerializeField] private LayerMask groundMask;
    [SerializeField] private float gravity;

    [SerializeField] private float jumpHeight;

    //REFERENCES
    private CharacterController controller;
    private Animator anim;
    private Collider Collider;
    private Rigidbody rb;
    private Transform tran;



    private void Start()
    {
    controller = GetComponent<CharacterController>();
    anim = GetComponentInChildren<Animator>();
    Collider = GetComponent<BoxCollider>(); ;
    rb = GetComponent<Rigidbody>();
    tran = GetComponent<Transform>();
    }

    private void Update()
    {
    Move();
    }

    private void Move()
    {
    isGrounded = Physics.CheckBox(Collider.bounds.center, Collider.bounds.extents, transform.rotation, groundMask);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }
    float moveZ = Input.GetAxis("Vertical");


    moveDirection = new Vector3(0, 0, moveZ);
    moveDirection = transform.TransformDirection(moveDirection);

    if (isGrounded)
    {
    velocity = Vector3.zero;
    if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
    {
    Walk();

    }
    else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
    {
    Run();
    }
    else if (moveDirection == Vector3.zero)
    {
    Idle();
    }

    moveDirection *= moveSpeed;
    if (isGrounded && Input.GetKeyDown(KeyCode.Space) && moveDirection == Vector3.zero )
    {
    Jump();
    }

    if (isGrounded && Input.GetKeyDown(KeyCode.Space) && Input.GetKey(KeyCode.W) && moveDirection != Vector3.zero)
    {
    JumpFor();
    }

    anim.SetBool("Jump", false);
    anim.SetBool("JumpUp", false);
    }

    if (!isGrounded)
    {
    anim.SetBool("Jump", true);
    }

    controller.Move(moveDirection * Time.deltaTime);

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
    }

    private void Idle()
    {
    anim.SetFloat("Speed", 0, 0.02f, Time.deltaTime);
    }

    private void Walk()
    {
    moveSpeed = walkSpeed;
    anim.SetFloat("Speed", 0.6f, 0.1f, Time.deltaTime);
    }

    private void Run()
    {
    moveSpeed = runSpeed;
    anim.SetFloat("Speed", 1, 0.1f, Time.deltaTime);
    }

    private void Jump()
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    private void JumpFor()
    {
    velocity = new Vector3(0, Mathf.Sqrt(jumpHeight * -2 * gravity), Mathf.Sqrt(jumpHeight * -2 * gravity));
    velocity= transform.TransformDirection(velocity);
    }
    }
    > `Blockquote`
     
  4. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Kurt-Dekker likes this.