[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;
                 }
             
         }
     }
 }
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
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;
             }
         }
     }
 }
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
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                