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 /
  • Help Room /
avatar image
0
Question by jesser1981 · Dec 13, 2016 at 12:40 PM · c#scripting beginnerspace shooterspaceshipspaceflight

[Solved] I am trying to build a space sim/ shooter type game.

I am trying to build a space sim/shooter type game kind of like elite dangerous but obviously much less feature because I am just starting out and learning, so I do not have the required experience or knowledge as of yet. I would like to add to it over timer as I learn new skills and gain knowledge. I have my movement script mostly working the way I would like but am having trouble with a couple things.

When the curSpeed is 0 I would like the ship to come to a complete stop, as of right now when I set the curSpeed to 0 in the inspector, it keeps moving either forward or backwords. The other thing is that I would like the speed to increment by maxAcceleration when the plus key is held down, or -maxAcceleration when the minus key is held down. As of right now it jumps by about 5 even when just tapping either button. I am including my script bellow. Any help would be greatly appreciated.

Ps. I am using Unity 5.5

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace Space_Shooter_Proto
 {
     [RequireComponent(typeof(Rigidbody))]
     public class ThrustTtest : MonoBehaviour
     {
         //Variables - Define Ship Movement
         private int maxSpeed = 150;
         private int minSpeed = 0;
         private int maxRevSpeed = -50;
         private int maxAcceleration = 1;
         public int curSpeed;
         private Rigidbody rb;
 
 
 
         void Awake()
         {
             rb = GetComponent<Rigidbody>();
         }
 
 
         // Use this for initialization
         void Start()
         {
 
         }
 
         // Update is called once per frame
         void Update()
         {
             ShipForwardThrust();
         }
 
         void ShipForwardThrust()
         {
             rb.AddForce(Vector3.forward * curSpeed * Time.deltaTime);
 
             if (Input.GetKey(KeyCode.Equals))
             {
                 curSpeed += maxAcceleration;
 
                 if (curSpeed == maxSpeed || curSpeed > maxSpeed)
                 {
                     curSpeed = maxSpeed;
                 }
 
             }
 
             if (Input.GetKey(KeyCode.Minus))
             {
                 ShipReverseThrust();
             }
 
             Debug.Log("Current Speed is " + curSpeed);
         }
 
         void ShipReverseThrust()
         {
             
             
                 curSpeed -= maxAcceleration;
 
                 if (curSpeed == maxRevSpeed || curSpeed < maxRevSpeed)
                 {
                     curSpeed = maxRevSpeed;
                 }
             
         }
     }
 }
Comment
Add comment · Show 1
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 UnityCoach · Dec 13, 2016 at 08:15 PM 0
Share

Hi, I don't mean to "advertise" here. I read that you're learning and would like to suggest you have a look at this training course, as it touches on everything you need to develop a game with Unity 5, and uses a space shooter for example. Looking at your question, I believe you'll like the chapter touching on the Ship Controller.

You'll find the free chapters here on youtube: https://www.youtube.com/channel/UCXSCcU$$anonymous$$O$$anonymous$$WQ54ujClJLWy5w The full training course is available in english here and in french here

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Landern · Dec 13, 2016 at 03:48 PM

Set the isKinematic(rb.isKinematic = true) to true and adjust the velocity to zero(rb.velocity = Vector3.zero), when greater than zero or less than zero(backwards) set the isKinematic to false(rb.isKinematic = false) and let the system adjust the velocity through AddForce. Also you should be using FixedUpdate instead of just Update for your physics operations.

Something like:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 namespace Space_Shooter_Proto
 {
     [RequireComponent(typeof(Rigidbody))]
     public class ThrustTtest : MonoBehaviour
     {
         //Variables - Define Ship Movement
         private int maxSpeed = 150;
         private int minSpeed = 0;
         private int maxRevSpeed = -50;
         private int maxAcceleration = 1;
         public int curSpeed;
         private Rigidbody rb;
 
         void Awake()
         {
             rb = GetComponent<Rigidbody>();
         }
 
 
         // Use this for initialization
         void Start()
         {
 
         }
 
         // Update is called once per frame
         void FixedUpdate()
         {
             ShipForwardThrust();
         }
 
         void ShipForwardThrust()
         {
             if (Input.GetKey(KeyCode.Equals))
             {
                 curSpeed += maxAcceleration;
 
                 if (curSpeed == 0)
                 {
                     rb.isKinematic = true;
                     rb.velocity = Vector3.zero;
                 }
                 else
                 {
                 if (rb.isKinematic == true)
                     rb.isKinematic = false;
                 }
                 
                 if (curSpeed == maxSpeed || curSpeed > maxSpeed)
                 {
                     curSpeed = maxSpeed;
                 }
 
             }
 
             if (Input.GetKey(KeyCode.Minus))
             {    
                 ShipReverseThrust();
             }
 
             rb.AddForce(Vector3.forward * curSpeed * Time.deltaTime);
 
             Debug.Log("Current Speed is " + curSpeed);
         }
 
         void ShipReverseThrust()
         {
             curSpeed -= maxAcceleration;
 
             if (curSpeed == 0)
             {
                 rb.isKinematic = true;
                 rb.velocity = Vector3.zero;
             }
             else
             {
                 if (rb.isKinematic == true)
                     rb.isKinematic = false;
             }
                 
             if (curSpeed == maxRevSpeed || curSpeed < maxRevSpeed)
             {
                 curSpeed = maxRevSpeed;
             }
         }
     }
 }
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 jesser1981 · Dec 13, 2016 at 11:06 PM 0
Share

Thank you very much for your help. I had a feeling that I would need to make it kinematic for it to come to a full stop at 0 but wasn't sure, and I had no clue had to add rb.velocity= Vector3.zero. Is there a way for increment the speed 1 only at all times? As of right now I can only get it to work on getkeydown, but I want it increase even if the key is held down.

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

278 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Project Space Shooter: My ship won't move! 2 Answers

Trail effect for non-moving object 2 Answers

How do you make your player jump in the Roll-a-Ball tutorial series? 0 Answers

How do I inactivate Text in a script? 2 Answers

How would I use other.gameobject like I've seen in collider scripts? to work in my camera script 0 Answers


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