Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by emilios1310 · Aug 23, 2014 at 06:11 AM · c#vehicleboatdriving

Boat movement, driving and animation

I'm trying to make a player-controllable boat and I'm running into problems with my code.

1) I want to make the boat to accelerate and decelerate steadily instead of simply moving the speed I'm telling it to right away.

2) I want to make the player unable to steer the boat unless it is moving.

3) I've animated an idle animation of my boat floating around, but when the animation is playing, my boat can't move. What can I do to eliminate that?

My current code (C#) is this:

using UnityEngine; using System.Collections;

 public class VehicleScript : MonoBehaviour {
     public float speed=10; 
     public float rotationspeed=50;
 
     // Use this for initialization
     // Update is called once per frame
     void Update () {
 
         // Forward movement
         if(Input.GetKey(KeyCode.I)) 
         transform.Translate (Vector3.left*speed*Time.deltaTime);
 
         // Backward movement
         if(Input.GetKey(KeyCode.K))
             transform.Translate (Vector3.right*speed*Time.deltaTime);
 
         // Left movement
         if(Input.GetKey(KeyCode.J))
             transform.Rotate (Vector3.down*rotationspeed*Time.deltaTime);
 
         // Right movement
         if(Input.GetKey(KeyCode.L))
             transform.Rotate (Vector3.up*rotationspeed*Time.deltaTime);
     
     }
 }

In the current state of my code, when I press the specified keys, the boat simply moves 10 units/sec instantly, and also stops instantly. I'm not really sure how to make the things stated above, so any help would be appreciated. Just to clarify, I don't necessarily need the full code to implement those features, I just want to know what functions to use in order to achieve the desired effects.

Thank you very much.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Aug 23, 2014 at 07:20 AM

This is a physics problem. You just need to store the velocity in a local variable, and add to it or subtract from it each frame. For instance, if they hit the forward button, you increase the local velocity variable by time.deltaTime*acceleration where acceleration is some constant you define. Then if they don't press anything, you would want to reduce the velocity, probably by a smaller deceleration.

Making the boat not move when it's standing still is easy. Just call Rotate(speed*angularRate*time.deltaTime) where angularRate is a constant, and speed is the magnitude of the previously computed velocity. So when the boat's velocity reaches 0, it stops rotating, and it actually rotates faster as the velocity increases. If it rotates too fast, use speed/2 or more. If you want it non-linear, sqrt(speed).

Lastly, Translate by the velocity vector.

So, in this order:

1) Increase/Decrease velocity based on forward/backward/no input

2) Limit velocity to some min and max

3) Rotate by the magnitude of the velocity based on left/right

4) Translate by the velocity

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image emilios1310 · Aug 26, 2014 at 08:49 AM 0
Share

Thank you very much, that solved my problem!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I need help with my pause menu 1 Answer

C# wait X-Seconds in Code before continue in Code? 3 Answers

How to assign transfrom array in the code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges