Created
November 20, 2017 18:20
-
-
Save LeviVisser/18783d4f9823097a5e0560d5ed6d7bd8 to your computer and use it in GitHub Desktop.
Ship engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Engine : MonoBehaviour { | |
[Range(100f, 1000f)] | |
public float Force; | |
[Range(10f, 20f)] | |
public float MaxSpeed; | |
public bool IsOn; | |
public float MaxEngineSpeed; | |
[Range(20f, 30f)] | |
public float MaxBoostSpeed; | |
[Range(1f, 2f)] | |
public float BoostModifier; | |
public Rigidbody ShipRigidBody; | |
public ShipMovement ShipMovement; | |
public float EngineForce; | |
public GameObject EngineModel; | |
// Use this for initialization | |
void Start () { | |
EngineModel.SetActive(false); | |
MaxSpeed = 5f; | |
EngineForce = 0; | |
} | |
void Update() | |
{ | |
if(EngineForce < MaxSpeed) | |
{ | |
MaxSpeed = MaxEngineSpeed; | |
} | |
} | |
// Update is called once per frame | |
void FixedUpdate () { | |
ApplyThrust(); | |
if (isOn) { | |
ShipRigidBody.AddForceAtPosition(transform.forward * Force * EngineForce, transform.position, ForceMode.Force); | |
} | |
} | |
void ApplyThrust() | |
{ | |
if (ShipMovement.AddThrust()) | |
{ | |
isOn = true; | |
EngineModel.SetActive(true); | |
EngineForce += 0.01f; | |
if(EngineForce >= MaxSpeed) | |
{ | |
EngineForce = MaxSpeed; | |
} | |
if (ShipMovement.ApplyBoost()) | |
{ | |
Boost(); | |
} | |
} | |
else if (!ShipMovement.AddThrust()) | |
{ | |
isOn = true; | |
EngineModel.SetActive(false); | |
EngineForce = EngineForce; | |
} | |
if (ShipMovement.RemoveThrust()) | |
{ | |
isOn = true; | |
EngineModel.SetActive(false); | |
EngineForce -= 0.01f; | |
if(EngineForce <= 0) | |
{ | |
EngineForce = 0; | |
} | |
} | |
} | |
void Boost() | |
{ | |
if (MaxSpeed < MaxBoostSpeed) | |
{ | |
MaxSpeed = BoostModifier * MaxSpeed; | |
} | |
else | |
{ | |
MaxSpeed = MaxBoostSpeed; | |
} | |
EngineForce = BoostModifier * EngineForce; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment