- Home /
Help on a basic script
Hello. I'm fairly new to Unity, and I'm trying to make a simple barebone Android puzzle game. It's a ball, which when you tilt, moves, and then puzzles and yada-yada-yadi-yada. So far I have this script on the ball: (a modification of the Unity script) #pragma strict // Credit to Unity for this script
// Move object using accelerometer
var speed = 10000.0;
function Update () {
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = Input.acceleration.x * 5;
dir.z = Input.acceleration.y * 5;
// clamp acceleration vector to unit sphere
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.rigidbody.AddForce(dir * speed);
transform.rigidbody.AddForce(dir * speed);
}
I'd like to add dampening or something similar, because currently this ball is in constant motion and that doesent allow for the player to have time to think. I'm also a bit confused about the speed, whatever I change it to it doesent change the actual speed. I made it add the force two times because that puts it at a semi-desirable level. I'd love any help.
Thanks.
Concerning your speed not changing, when you make a public variable, its value is used only the first time you attach the script. After that, you modify via the inspector. If you want to modify a public variable within the script and you want the changes to be applied, you need to click the little wheel on the top right of the component in the inspector and click Reset. That will set your component to script values.
Answer by Spinnernicholas · Nov 26, 2013 at 11:15 PM
For a better simulation, why don't you just modify gravity to mimic the real world.
First, create a floor(and maybe some walls) for the ball to sit on and set its:
Rigidbody.useGravity = false;
Rigidbody.constraints = RigidbodyConstraints.FreezeAll;
Then add gravity based on acceleration:
void Update()
{
Physics.gravity = new Vector3(Input.acceleration.x * 5, 0, Input.acceleration.y * 5);
}
Lastly, Modify the PhysicMaterials and masses of the ground and ball for the desired effect.
That sounds like a great idea, but what about puzzle elements that you need to push? (e.x. boxes and buttons that use boxes)
Well, either you can use the physic material to make it so the blocks and stuff don't slide.
Or, you can leave the global gravity alone and only change the gravity on the ball.
Answer by belvita · Dec 01, 2013 at 09:11 AM
Try this
this.gameObject.rigidbody.AddForce(transform.right*50);
Or change transform.right to ur required direction