- Home /
Make a object move forwards
I am trying to make an object move forwards, so I used this bit of code:
rigidbody.AddForce(Vector3.forward * speed, ForceMode.Force);
But it will only move in the Z direction. I read online that Vector3.forward is just shorthand for the Z axis so that makes sense, but is there a way of moving in the actual forward direction? Also I have one more question/bug, after it moves a ways it will start going really slowly, even when the speed is supposedly still at a high number.
Here is the whole script:
#pragma strict
var speed : float;
var maxSpeed : float;
var acceleration : float;
function Start () {
}
function Update () {
}
function FixedUpdate(){
if(Input.GetKey("w")){
if(speed < maxSpeed){
speed += acceleration;
rigidbody.AddForce(Vector3.forward * speed, ForceMode.Force);
}
}
if(Input.GetKey("a")){
transform.Rotate(-Vector3(0, 1, 0));
}
if(Input.GetKey("d")){
transform.Rotate(Vector3(0, 1, 0));
}
}
Answer by moxiewhimsy · Mar 26, 2014 at 05:26 PM
replace
rigidbody.AddForce(Vector3.forward * speed, ForceMode.Force);
with
rigidbody.AddForce(transform.forward * speed, ForceMode.Force);
You want the object's forward and not the world's forward.
Thanks that worked great. The only thing now is that the boat kindof rotates to the right a bit on its own. Also it glitches into another rigidbody. Any fix you know for that? Thanks a bunch!
Nvm, i fixed the glitching part, but how would i make it so that it doesnt drift? Is there an easy way to do that? Like transferring that AddForce to go to the different "forward" so it doesnt drift?
Not sure. It might be drifting for a few reasons. Is your water technically a solid (colliding with the boat constantly), or does it have some sort of buoyancy physics? How much drag does the boat have? Etc. I don't know all the ins and outs.
@nicedude80: By "drift" do you mean momentum when you change direction? In real-life we have something we call friction. In "fluids" which includes air you call it Drag. You can simulate drag by setting a drag value on your rigidbody. $$anonymous$$eep in $$anonymous$$d the higher the drag value the faster it will decelerate and the more force you need to make it move.
Your answer
Follow this Question
Related Questions
Applying Constant Force on Multiple Axes Via Javascript? 1 Answer
Making a flight script dependent on several variables activated by another script. 1 Answer
when i try using .SimpleMove my object jumps back to its starting position 0 Answers
Ball getting stuck in surfaces and bouncing for no reason? Help! 0 Answers