I have a problem with the bool variable landShip i can’t figure out how to make it like a switch in the Update function and when i click on the L key. I want that when i click on the L key and only when it’s getting back to it’s originalPosition then stop the object.
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour
{
public int rotationSpeed = 75;
public int movementspeed = 10;
public int thrust = 10;
private bool isPKeyDown = false;
private float acceleration = .0f;
private Vector3 previousPosition = Vector3.zero;
private Rigidbody _rigidbody;
private Vector3 originalPosition;
private Quaternion originalRotation;
private bool landShip = false;
// Use this for initialization
void Start()
{
originalPosition = transform.position;
originalRotation = transform.rotation;
_rigidbody = GetComponent<Rigidbody>();
Debug.Log("Acc Speed: " + thrust);
}
// Update is called once per frame
void Update()
{
var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
if (landShip == false)
{
transform.position += transform.forward * Time.deltaTime * movementspeed;
}
if (Input.GetKey(KeyCode.Z))
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
if (Input.GetKey("p"))
{
isPKeyDown = Input.GetKey("p");
float distance = Vector3.Distance(previousPosition, transform.position);
acceleration = distance / Mathf.Pow(Time.deltaTime, 2);
previousPosition = transform.position;
_rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
}
if (Input.GetKey("l"))
{
landShip = true;
}
if (landShip == true)
{
StartCoroutine(TurnShip(transform, transform.eulerAngles, new Vector3(0, 180, 0), 1));
if (transform.position == originalPosition)
{
Debug.Log("At original position " + originalPosition);
//landShip = true;
}
}
}
IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth)
{
float lerpSpeed = 0;
while (lerpSpeed < 1)
{
ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed);
lerpSpeed += Time.deltaTime * smooth;
yield return null;
}
}
void OnGUI()
{
if (isPKeyDown)
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
}
}
}
The problem is here:
[code]
if (landShip == false)
{
transform.position += transform.forward * Time.deltaTime * movementspeed;
}
And here
if (Input.GetKey("l"))
{
landShip = true;
}
if (landShip == true)
{
StartCoroutine(TurnShip(transform, transform.eulerAngles, new Vector3(0, 180, 0), 1));
if (transform.position == originalPosition)
{
Debug.Log("At original position " + originalPosition);
//landShip = true;
}
}
Can’t figure out how to use the landShip bool.