How to move

Updated 5 days ago in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on July 30, 2018

I want my boat in my game to move only forward when you press w or up arrow key and Not backwards when you press s or down arrow key.  I also want to rotate left when you press a or the left arrow key and turn right when you press d or the right arrow key.

  • Liked by
Reply
0 on July 30, 2018

Assuming you’re using GetAxis, and rigidbodies for movement and rotation,

 

You can stop going backwards by clamping the forward/backwards input (vertical) between 0, and 1. As opposed to -1, 1 it is by default;

float forwardForce = Input.GetAxis("Vertical");
forwardForce = Mathf.Clamp(forwardForce, 0f, 1f);
rb.AddForce(transform.forward * forwardForce);

 

For rotation, you don’t need to clamp it because it can rotate CW and CCW.

float turn = Input.GetAxis("Horizontal");
rb.AddTorque(transform.up * turn);

 

Keep in mind, that both of these examples are only going to move at most 1 unit or degree per frame, not counting rigidbody drag or deltaTime. SOOOoooo you’ll probably want to multiply them by some speed variable to increase or decrease them.

  • Liked by
Reply
Cancel
1 on July 31, 2018

 

Thanks a lot!  That really help me, but I have a couple of questions.  First question is what is CW and CCW.  Second, what is AddTorque?  Last question, why do you need the transform.up for rotating left or right.  You have been a big help  and I also I want the boat to accelerate to a certain max speed and I was wondering how I could do that! 

on July 31, 2018

No problem

 

CW = Clockwise (turn to the right)

CCW = Counter-clockwise (turn to the left)

AddTorque is the rigidbody equivalent of rotating.

We use transform.up because that’s the axis we’re rotating around

 

To have a max speed; just check the rigidbody’s current velocity, and if it’s over the limit you want, add more force to it, but backwards.

Show more replies
  • Liked by
Reply
Cancel
1 on August 4, 2018

How would you get collision to work without my boat flying and jumping around?  This worked and I thank you for that!

on August 5, 2018

Try increasing the RigidBody’s mass and drag.

Mass may make it a bit harder to move, so you may have to up your speed, and drag should just help keep it grounded.

Start small, like maybe 10 mass and only 1 drag.

 

Show more replies
  • Liked by
Reply
Cancel
1 on August 8, 2018

 

Do you need to use gravity?

Helpful
on August 9, 2018

Gravity is already applied to rigid bodys, did you give the objects (boat and obstacles) the right colliders to collide with?

Show more replies
  • Liked by
Reply
Cancel
0 5 days ago

I fixed the problem!  I forgot what I did but its working now!

  • Liked by
Reply
Cancel