Movement script not working properly

Hi,

My friends and I are making a 3D game (on C#) which consists of a ball going through obstacles to get to the finish line. The ball is controlled by the tilting of the phone and if you tap the screen, you jump.
I have the script which uses tilt and makes the ball move, and is working:

using UnityEngine;
using System.Collections;
 
public class TiltControl : MonoBehaviour
 
{
    public float speed = 10.0F;
    void Update()
    {
        Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, -Input.acceleration.z);
        rigidbody.velocity = movement * speed;
 
        if (Input.GetMouseButtonUp(0))
        {
            transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
        }
    }
 
    void OnTriggerEnter (Collider other)
    {
        if (other.gameObject.tag == "pickup")
        {
            other.gameObject.SetActive (false);
        }
    }
}

And I also have the script which makes the ball jump once the touchscreen is tapped:

using UnityEngine;
using System.Collections;
 
public class Jump : MonoBehaviour
{
    public float JumpSpeed = 10;
    public float gravity = 9.8f;
    CharacterController controller;
    Vector3 currentMovement;
 
    void Start ()
    {
        controller = GetComponent<CharacterController> ();
    }
 
    // Update is called once per frame
    void Update ()
    {
        if (!controller.isGrounded)
            currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
 
        if (controller.isGrounded && Input.GetMouseButtonUp(0))
        {
            currentMovement.y = JumpSpeed;
        }
 
        controller.Move (currentMovement * Time.deltaTime);
 
    }
}

But when I try to put them in one script:

using UnityEngine;
using System.Collections;
 
public class TiltControl : MonoBehaviour
 
{
    public float JumpSpeed = 10;
    public float gravity = 9.8f;
    public float speed = 10.0f;
    CharacterController controller;
    Vector3 currentMovement;
 
    void Start ()
    {
        controller = GetComponent<CharacterController> ();
    }
 
    void Update()
    {
       
        currentMovement = new Vector3 (Input.acceleration.x, -gravity * Time.deltaTime, -Input.acceleration.z);
 
        if (controller.isGrounded && Input.GetMouseButtonUp(0))
        {
            currentMovement.y = JumpSpeed;
        }
         
        controller.Move (currentMovement * Time.deltaTime);
         
    }
 
    void OnTriggerEnter (Collider other)
        {
            if (other.gameObject.tag == "pickup")
            {
                other.gameObject.SetActive (false);
            }
        }
}

It just doesnt work.

Can you please tell me what might be wrong?

Thank you.

This script uses the Rigidbody’s built-in AddForce feature to simulate the physics.

Since I don’t have a mobile device set up, I used #if statements and keyboard control. Just remove the lines with # and Input.GetAxis.

using UnityEngine;

public class TiltControl : MonoBehaviour
{
    public float moveForce = 5f;
    public float jumpImpulse = 5f;

    private bool isGrounded = false;

    void FixedUpdate()
    {
        Rigidbody rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            Vector3 force = new Vector3(moveForce, 0f, moveForce);
#if UNITY_EDITOR
            force.x *= Input.GetAxis("Horizontal");
            force.z *= Input.GetAxis("Vertical");
#else
            force.x *= Input.acceleration.x;
            force.z *= Input.acceleration.z;
#endif
            rb.AddForce (force); // gradual change in velocity

            if (isGrounded && Input.GetMouseButtonUp(0))
            {
                rb.AddForce(new Vector3(0f, jumpImpulse, 0f), ForceMode.Impulse); // instant change in velocity
            }
        }
    }

    void OnCollisionEnter(Collision data)
    {
        if (data.gameObject.tag == "ground")
        {
            isGrounded = true;
        }
    }
    void OnCollisionExit(Collision data)
    {
        if (data.gameObject.tag == "ground")
        {
            isGrounded = false;
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "pickup")
        {
            other.gameObject.SetActive(false);
        }
    }
}