- Home /
Make an object move forward on its own
Im working on a game for the iphone where you tilt the screen to move a character but i want the character to move forward really fast on its own. Sounds kinda lame but i need help getting the player to move forward on its own. I tried rigidbody.AddForce and something that went like rigidbody.velocity(transform.position * 10) but it didnt work. Any suggestions at all would help
Answer by Phil 5 · Jan 28, 2011 at 02:27 PM
Here two samples, one without and one with physics. If you use the physics one, be sure you have a rigidbody attached and a collider. Be sure the Drag of the rigidbody is 0.
float speed = 2.0f;
//Either this void Update() { transform.position += transform.forward speed Time.deltaTime; //I don't recall if "transform.position += something", if not, //user "transform.position = transform.position + something" }
//or that void FixedUpdate() { //this rigidbody.AddForce(transform.forward speed, ForceMode.VelocityChange); //or that for physics, but it's not the best way rigidbody.velocity = transform.forward speed; }
If you don't need physics in your game, I really suggest you the first solution to increase performance on iphone. I used C# but it should be easy to make it in javascript if you use that.
Answer by Myth · Jan 28, 2011 at 05:08 AM
I am a bit of a noob in unity but,
for a constant movement have you considered something like this:
count = count + speed;
transform.position = Vector3(xpos, ypos, zpos+count);
hi,
the script above helped me to get my gameobject move constantly. however, it'll keep on going on constant speed throughout the entire time. do u have any idea how to attach the speed value to a slider? my goal is to manouvre the moving speed fast and slow depending on users navigation.
thank you in advance,
cio
@ciooong it's simple. You would need to add a value from the slider, I'll try something. This is untested by the way so it may not work, it'll be something along these lines. // THIS IS JUST FOR THE FUNCTION AND IS IN C#
public float speed;
public float count;
void adjustSpeed (float speed) {
count += speed;
transform.position = Vector3(xpos,ypos,zpos+count);
}