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;
}
}
}
}
Landern
2
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;
}
}
}
}