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
1
Question by unity_PM96A4-pjeC-4Q · Nov 26, 2017 at 07:46 PM · transformunity 2dspeedspacespaceship

Making Top-Down spaceship movement, getting current speed, acceleration, without rigidbody!

So I have a simple movement script. My ship rotates on "A and D" and geting maximum speed with"W/S". But, I need so my ship gets to maximum speed not instantly, and wont stop instantly. The thing is, I dont want to use RigidBody2d. My thinkin is : 1) I need somehow get current ship speed, 2) move it with this speed every frame 3) add speed when buttun pressed 4) slowly decrease speed, when nothing pressed

Can someone help whith this? (Sorry for language)

using UnityEngine; using System.Collections;

 public class PlayerMovement2 : MonoBehaviour {
 
 
     public float maxVerticalSpeed;
     public float maxRotationSpeed;
     public float Speed;
 
 
     void Start () {
 
     
     }
     
     void FixedUpdate () {
 
     transform.Translate (new Vector3 (0, Input.GetAxis ("Vertical") * maxVerticalSpeed * Time.deltaTime));
     transform.Rotate (new Vector3 (0, 0, -1 * Input.GetAxis("Horizontal") * maxRotationSpeed * Time.deltaTime));
 
 
     }
 }
     
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
4
Best Answer

Answer by villevli · Nov 27, 2017 at 12:22 AM

Here's a simple script which accelerates the object with input and drag is applied each FixedUpdate to slow the object down.

 public class ControlledRigidbody2D : MonoBehaviour
 {
     public float verticalInputAcceleration = 1;
     public float horizontalInputAcceleration = 20;
 
     public float maxSpeed = 10;
     public float maxRotationSpeed = 100;
 
     public float velocityDrag = 1;
     public float rotationDrag = 1;
 
     private Vector3 velocity;
     private float zRotationVelocity;
 
     private void Update()
     {
         // apply forward input
         Vector3 acceleration = Input.GetAxis("Vertical") * verticalInputAcceleration * transform.up;
         velocity += acceleration * Time.deltaTime;
 
         // apply turn input
         float zTurnAcceleration = -1 * Input.GetAxis("Horizontal") * horizontalInputAcceleration;
         zRotationVelocity += zTurnAcceleration * Time.deltaTime;
     }
 
     private void FixedUpdate()
     {
         // apply velocity drag
         velocity = velocity * (1 - Time.deltaTime * velocityDrag);
 
         // clamp to maxSpeed
         velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
 
         // apply rotation drag
         zRotationVelocity = zRotationVelocity * (1 - Time.deltaTime * rotationDrag);
 
         // clamp to maxRotationSpeed
         zRotationVelocity = Mathf.Clamp(zRotationVelocity, -maxRotationSpeed, maxRotationSpeed);
 
         // update transform
         transform.position += velocity * Time.deltaTime;
         transform.Rotate(0, 0, zRotationVelocity * Time.deltaTime);
     }
 }
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 unity_PM96A4-pjeC-4Q · Nov 27, 2017 at 03:11 PM 0
Share

Omg, you just saved my project! <3

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

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Advice on my Ship AI... 4 Answers

How to check if one GameObject is in the way of another GameObject? 1 Answer

Manipulating cursor direction in 3D space 0 Answers

Referencing a gameobject in another scene 1 Answer

Move child closer to parent ? 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