Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Doctor06 · Jan 24, 2016 at 09:55 PM · positionmove an objectacceleration

Ship rotation while moving forward

I am making ship move to mouse click. The ship goes to where i clicked, and the ship even rotates before it moves towards the click. The only problem i see is that when the ship moves forward, moves from speed 0 to the speed i set it too. I have tried using Lerp to accelerate smoothly before moving and slow down before stopping. The ship however takes the same amount of time to get from point A to point B regardless of distance, so things look funny.

If someone could help me out with this, that would be awesome. I need to find a way to add acceleration to the ship. And for a plus, it would be amazing if you guys could tell me how to make the ship move forward so the ship makes a small semi circle before heading towards the direction i clicked.

Here is the code i have so far.

 public float speed;
 private Vector3 endPoint;
 private Quaternion rotation;
 float tempTime;
 bool start_time = false;
 float half_way;

 void Update() {
     float step = speed * Time.deltaTime;
     // if left mouse clicked
     if (Input.GetMouseButton (0)) {
         endPoint = Input.mousePosition;
         endPoint.z = 20.0f; // position of main camera on the z axis
         //get endpoint relative to the camera
         endPoint = Camera.main.ScreenToWorldPoint(endPoint);
         // set rotation value
         rotation = Quaternion.LookRotation (endPoint - transform.position);
         tempTime = 0.0f;
         start_time = true;
         half_way = (endPoint - transform.position).magnitude / 2;
     }

     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, (Time.deltaTime * speed / 4));
     if (start_time) {
         tempTime += Time.deltaTime / 8;
     }
     // if rotation is half way done.
     if (tempTime > step){
         //get direction
         Vector3 dir = endPoint - transform.position;
         // distance from A to B
         float distance = dir.magnitude;
         if (distance > step) {
             transform.position += dir.normalized * step;
         } else {
             transform.position = endPoint;
             transform.rotation = rotation;
             start_time = false;
         }
     }
 }

Thank you in advance.

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 lgarczyn · Jan 24, 2016 at 10:26 PM

use http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# move a cube slowly 1 Answer

Spaceship movement with acceleration and deceleration on Unity 0 Answers

Scrolling Sprite Object 0 Answers

Moving child and parent 1 Answer

Move object up and down while moving forward 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