Build Roll a ball for android or iOS in Unity

Build Roll a ball for android or iOS in Unity
December 3, 2018 No Comments Android Development,Game Development,iOS Development Pushpendra Kumar



Have you done with the standalone programming? Yes!

If you are looking for this issue then I make sure then you have done with the official unity tutorial. In the official tutorial they have described everything but at the end, they have missed how you can run your game on the android device.

For the official tutorial “CLICK HERE”

There is small code for android or iOS device which make your game running into the touch device. So let’s have look on that code!.

This is the FixedUpdate() function code. You will find FixedUpdate() into your PlayerController.

private void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.AddForce(movement * speed);
    }

Here add one more function below that!.

void OnTouchDevice()
    {
        float moveHorizontal = Input.acceleration.x;
        float moveVertical = Input.acceleration.y;
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.AddForce(movement * speed * 2);
    }




Call this function from the FixedUpdate(). Now your FixedUpdate() will look like –

private void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.AddForce(movement * speed);
        OnTouchDevice()
    }

This change will perfectly work for your device!.
Note : This is just a normal change for more deeply you can try a different kind of modification as per your requirement. Try those and enjoy that.

Good Luck 🙂 Happy Coding

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *