Add Tilt Function To Rotate a Ball

Updated on July 25, 2014 in [A] Other Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on July 25, 2014

I’m new at unity and i’m using the tutorial “make a game by Brackeys”.

I had completed all the step but now, i’m wonder how can i add tilt function to that game to rotate that ball in android device. i had searched alot but nothing found.

I’m using the code that you have provided to us. Can you please help to add the tilt function and send me the code????

  • Liked by
Reply
0 on July 25, 2014

Can you post the scripts’ code here, so people who don’t have the tutorial on hand can look at it.

  • Liked by
Reply
Cancel
0 on July 25, 2014

Here is the code that i m using…:)

#pragma strict

var rotationSpeed = 1000;
var jumpHeight = 8;
var tilt : Vector3 = Vector3.zero;

private var isFalling = false;

function Update ()
{
    
    var rotation : float = Input.GetAxis (“Horizontal”) * rotationSpeed;
    rotation *= Time.deltaTime;
    rigidbody.AddRelativeTorque (Vector3.back * rotation);
    rigidbody.AddRelativeTorque(tilt * rotation);
    
    if((Input.GetKeyDown(KeyCode.Mouse0) || Input.GetKeyDown(KeyCode.UpArrow)) && isFalling == false)
    {
        rigidbody.velocity.y = jumpHeight;
    }
    isFalling = true;
}
function OnCollisionStay()
{
    isFalling = false;
}

  • Liked by
Reply
Cancel
0 on July 25, 2014

Can any One help me????

  • Liked by
Reply
Cancel
0 on July 25, 2014

?

  • Liked by
Reply
Cancel
0 on July 25, 2014

I don’t have enough time to fix your code up but this is the Unity command/control for Tilting.

  • Liked by
Reply
Cancel
0 on July 25, 2014

First you need to get the x axis when you tilt the phone, then you apply the rotation of the ball to that axis.
Here’s a tutorial so you can learn how the accelerometer works.
Accelerometer Input

  • Liked by
Reply
Cancel