- Home /
tilt device , roll the ball
hello all, i am 3d cg artist, i am new to unity, and need help from you.
i am trying to make a simple game "labyrinth" my logic is when player tilt the device , ball rolls appropriate, i have created 3d assets for this in unity, can handle physics a bit, but low on scripting, can anybody helps me out, from start what to do ? thanks.
i am applying this script on ball, but when runs the build on device nothing happens :-(
pragma strict
var tilt : Vector3 = Vector3.zero; var speed : float; private var circ : float; private var previousPosition : Vector3;
@script RequireComponent(Rigidbody) function Start() { //Find the circumference of the circle so that the circle can be rotated the appropriate amount when rolling circ = 2 Mathf.PI collider.bounds.extents.x;
previousPosition = transform.position; }
function Update () { tilt.x = -Input.acceleration.y; tilt.z = Input.acceleration.x;
rigidbody.AddForce(tilt * speed * Time.deltaTime);
}
function LateUpdate() { var movement : Vector3 = transform.position - previousPosition;
movement = Vector3(movement.z,0, -movement.x);
transform.Rotate(movement / circ * 360, Space.World);
previousPosition = transform.position;
}
Hard to see when your code is not formatted properly, but what value have you assigned to speed in the inspector? Also if you're using rigidbody physics, there should be no need to do your manual rotation in LateUpdate - let the physics engine sort out the rotation for you.
i got your point, but what logic or script do i use to tilt? i mean physics will run my ball, but what action will force the ball to do action?
What I'm saying is that if you're using Input.acceleration to apply a rigidbody force to the ball to move/rotate it, you don't also need try to manually calculate the rotation of the ball. Basically I think your LateUpdate() function is unnecessary.
But that's not the answer to your question. What value did you assign to speed?
Answer by fafase · Nov 06, 2013 at 09:19 AM
Try to apply the Input.acceleration to the rigidbody.velocity:
rigidbody.velocity = tilt * speed;
You may need some values to make the object move to theappropriate speed but that is what we use for similar purpose.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
If Statement Conditional Not Working 1 Answer
Stop movement at edge of screen! 2d shooter 4 Answers