Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to make Rigidbody First Person Jump with button or mouse click?

Discussion in 'Scripting' started by rmills1997, Oct 13, 2019 at 1:29 AM.

  1. rmills1997

    rmills1997

    Joined:
    Saturday
    Posts:
    2
    I am making a mobile version of my game and I have a joystick and panel with scripts that allow the standard RigidbodyFirstPerson move and rotate with some tweaking of their original code (adding RunAxis and JumpAxis) but as many times as I've tried I can't get the jump to work, I don't think the code works for it. I followed this video
    and have spent so long trying to get this darn thing to work. If I can get any suggestions I would really appreciate it. Is there another script I could create to just make it jump with a mouseclick
     
  2. rmills1997

    rmills1997

    Joined:
    Saturday
    Posts:
    2
    I ended up taking all the jump scripting from this tutorial and tried my own code. It doesnt work either, this is it

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.EventSystems;
    public class MoveUp : MonoBehaviour
    {
    public Rigidbody rb;
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    public void OnPointerDown(PointerEventData eventData)
    {
    transform.Translate(0, 10, 0);
    }
    }

    Im new to scripting, probably doing it completely wrong but I thought there might be a way to do it like this, but not sure
     
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    95
    First, please use code block. It's that "Code: " section in the editor. Makes it so much readable and doesn't require much more than cut'n'pasting the code as normal text here.

    Please open the reference manual and read the RigidBody section. Generally you want to move RigidBodies so that you apply move forces, like this:

    Code (CSharp):
    1. rigidbody.AddForce(new Vector3(0, 1000, 0), ForceMode.Impulse);
    There are different ForceModes than you can apply, you find them in the manual.
    https://docs.unity3d.com/ScriptReference/ForceMode.html

    If your RigidBody is in kinematic mode (isKinematic true or switched on in the Inspector), you can use RigidBody.MovePosition:
    https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

    Transform.translate might create some artifacts/jittery movement.

    You might find article useful to get an overall picture about differences of Character Controller and ra RigidBody character controller:
    https://medium.com/ironequal/unity-character-controller-vs-rigidbody-a1e243591483
     
    Last edited: Oct 13, 2019 at 1:29 PM