Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by rmills1997, Oct 13, 2019.

  1. rmills1997

    rmills1997

    Joined:
    Oct 12, 2019
    Posts:
    4
    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:
    Oct 12, 2019
    Posts:
    4
    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:
    1,553
    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