Ball moves too far when it jumps. It can still be controlled while its in air. Unity3d player control script

StackOverflow https://stackoverflow.com/questions/22618709

  •  20-06-2023
  •  | 
  •  

Question

I am working on unity ball game. My player is a ball and it uses a player control script. When the ball jumps in air, it can still be controlled and mo move to any direction while its in air. I do not want that as it fails the purpose of heaving a maze since it can fly above obstacles.

I am using a player control script that came with a free unity game kit. I have tried to fix it, but I am only capable of either removing the jump function or reducing its height, and could not fix the issue.

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour 
{
    private GameObject moveJoy;
    private GameObject _GameManager;
    public Vector3 movement;
    public float moveSpeed = 6.0f;
    public float jumpSpeed = 5.0f;
    public float drag = 2;
    private bool canJump = true;

    void Start()
    {
        moveJoy = GameObject.Find("LeftJoystick");
        _GameManager = GameObject.Find("_GameManager");
    }

    void Update () 
    {   
        Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
        forward.y = 0;
        forward = forward.normalized;

        Vector3 forwardForce = new Vector3();
        if (Application.platform == RuntimePlatform.Android) 
        {
            float tmpSpeed = moveJoy.GetComponent<Joystick>().position.y;
            forwardForce = forward * tmpSpeed * 1f * moveSpeed;
        }
        else
        {
            forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;
        }
        rigidbody.AddForce(forwardForce);

        Vector3 right= Camera.main.transform.TransformDirection(Vector3.right);
        right.y = 0;
        right = right.normalized;

        Vector3 rightForce = new Vector3();
        if (Application.platform == RuntimePlatform.Android) 
        {
            float tmpSpeed = moveJoy.GetComponent<Joystick>().position.x;
            rightForce = right * tmpSpeed * 0.8f * moveSpeed;
        }
        else
        {
            rightForce= right * Input.GetAxis("Horizontal") * moveSpeed;
        }       
        rigidbody.AddForce(rightForce);

        if (canJump && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(Vector3.up * jumpSpeed * 100);
            canJump = false;
            _GameManager.GetComponent<GameManager>().BallJump();
        }
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.tag == "Destroy")
        {
            _GameManager.GetComponent<GameManager>().Death();
            Destroy(gameObject);
        }
        else if (other.tag == "Coin")
        {
            Destroy(other.gameObject);
            _GameManager.GetComponent<GameManager>().FoundCoin();
        }
        else if (other.tag == "SpeedBooster")
        {
            movement = new Vector3(0,0,0);
            _GameManager.GetComponent<GameManager>().SpeedBooster();
        }
        else if (other.tag == "JumpBooster")
        {
            movement = new Vector3(0,0,0);
            _GameManager.GetComponent<GameManager>().JumpBooster();
        }
        else if (other.tag == "Teleporter")
        {
            movement = new Vector3(0,0,0);
            _GameManager.GetComponent<GameManager>().Teleporter();
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (!canJump)
        {
            canJump = true;
            _GameManager.GetComponent<GameManager>().BallHitGround();
        }
    }

    void OnGUI()
    {
        GUI.Label(new Rect(300,10,100,100),"X: " + moveJoy.GetComponent<Joystick>().position.x.ToString());
        GUI.Label(new Rect(300,30,100,100),"Y: " + moveJoy.GetComponent<Joystick>().position.y.ToString());
    }
}

The question has been answered. Now how to use this script -> Create a sphere and give it "Sphere Collider", "Mesh Renderer", "Rigidbody", "Player Control(Script)" Under player control script put this script and your done. Now you have a ball that can be controlled in ios,android and pc i guess and can jump.

Was it helpful?

Solution

I think canJump flag says "Player on the ground". So, if you "can't jump", that means you shouldn't allow gamer to control the character as it is flying. Check it in very start of Update() and call return; if canJump == false

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top