How can i make my spaceship to land automatic on the Base ?
What i want to do is like in this video: The video is made in blender i don't want the graphics but only the spaceship to land like that way.
This is my script:
using UnityEngine;
using System.Collections;
public class ControlShip : MonoBehaviour {
public int rotationSpeed = 75;
public int movementspeed = 10;
public int thrust = 10;
public float RotationSpeed = 5;
private bool isPKeyDown = false;
private float acceleration = .0f;
private Vector3 previousPosition = Vector3.zero;
private Rigidbody _rigidbody;
private bool landing = false;
private Vector3 originPosition;
private Vector3 lastPosition;
private const float minDistance = 0.2f;
private Transform baseTarget;
private float distanceTraveled;
private Vector3 targetAngles;
// Use this for initialization
void Start () {
baseTarget = GameObject.Find("Base").transform;
originPosition = transform.position;
_rigidbody = GetComponent<Rigidbody>();
Debug.Log("Acc Speed: " + thrust);
}
// Update is called once per frame
void Update()
{
if (landing == false)
{
var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
transform.position += transform.forward * Time.deltaTime * movementspeed;
if (Input.GetKey(KeyCode.Z))
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.R))
transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.P))
{
isPKeyDown = Input.GetKey(KeyCode.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);
}
}
else
{
transform.position += transform.forward * Time.deltaTime * movementspeed;
/*distanceTraveled += Vector3.Distance(transform.position, lastPosition);
if (distanceTraveled >= 50)
{
targetAngles = transform.eulerAngles + Random.Range(90.0f, 180.0f) * transform.up;
StartCoroutine(TurnShip(transform, transform.eulerAngles, targetAngles, 1f));
distanceTraveled = 0;
landing = false;
}*/
var targetRotation = Quaternion.LookRotation(baseTarget.position - transform.position);
var str = Mathf.Min(.5f * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
}
if (landed == true)
TakeOff();
if (Input.GetKey(KeyCode.L))
{
// Automatic landing
landing = true;
lastPosition = transform.position;
}
}
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 OnTriggerEnter(Collider other)
{
if (landing == true && other.gameObject.name == "Base")
{
StartCoroutine(Landed());
}
}
bool landed = false;
IEnumerator Landed()
{
yield return new WaitForSeconds(5);
Debug.Log("Landed");
landed = true;
}
private void TakeOff()
{
if (transform.position != originPosition)
{
_rigidbody.AddForce(transform.up * 10);
}
if ((transform.position - originPosition).sqrMagnitude <= (1f * 1f))
{
landed = false;
_rigidbody.useGravity = false;
}
}
void OnGUI()
{
if (isPKeyDown)
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
}
}
}
When i click/press once the L key it's starting the automatic landing. And getting to this part in the code:
transform.position += transform.forward * Time.deltaTime * movementspeed;
/*distanceTraveled += Vector3.Distance(transform.position, lastPosition);
if (distanceTraveled >= 50)
{
targetAngles = transform.eulerAngles + Random.Range(90.0f, 180.0f) * transform.up;
StartCoroutine(TurnShip(transform, transform.eulerAngles, targetAngles, 1f));
distanceTraveled = 0;
landing = false;
}*/
var targetRotation = Quaternion.LookRotation(baseTarget.position - transform.position);
var str = Mathf.Min(.5f * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
I tried first the part with the TurnShip and then now the part without it in both ways it's not even close to what i want.
This is a sort video clip i recorded showing how my spaceship start landing but in the end near the red base it's getting lost:
Answer by Manpreet_96 · Jun 23, 2017 at 05:24 PM
Well @haimmoshe that's a great amount of code you have written there. I didn't read it whole but I can suggest an approach. Simply just move the ship above base and then restrict it's movement in x-y plane and allowing it land smoothely along z. You can freeze x-y or whatever way you would like. If you really need a coding help here, just mark out the portion of your code you need help with. Plus do tell what are you using to check if plane has landed in base or not. Sorry, I really just skipped the lengthy code. Enjoy!! :P
This is the part for the landing. When i click/press the L key once the spaceship rotating facing the baseTarget and move to it. Then when it's above the base in specific height i'm using Raycast to make the spaceship get ready be in the right rotation above the base then it should move down and land.
I want to add also that when the spaceship rotate and move to the target base that it will slow down each time it's getting closer and closer to the target and not just to move directly to the baseTarget.
if (Physics.Raycast(transform.position, baseTarget.position, out hit, maxDistance))
{
transform.rotation = Quaternion.identity;
_rigidbody.useGravity = false;
}
else
{
//transform.position += transform.forward * Time.deltaTime * movementspeed;
float distance = Vector3.Distance(transform.position, baseTarget.position);
var targetRotation = Quaternion.LookRotation(baseTarget.position - transform.position);
var str = $$anonymous$$athf.$$anonymous$$in(.5f * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str);
if (distance <= 700)
{
//transform.position = Vector3.SmoothDamp(transform.position, baseTarget.position, )
}
}
I set the maxDistance in the Raycast past to 700. And in the second part i want it to slow down each time more a bit until it's getting above the baseTarget and then to make the Raycast part.
$$anonymous$$y logic:
Click on L key: Spaceship rotate to baseTarget direction and move to this direction with a slowdown according to the distance and speed between the spaceship(transform) and the baseTarget.
When the spaceship is above the baseTarget at height 700 use the Raycast to make the spaceship in the right position above the baseTarget then make the spaceship move down vertical down to the baseTarget.
O$$anonymous$$ @Chocolade I saw your video again and again, and if you notice that your whole code is working fine but hits problem just after landing, like it's trying to go somewhere else. Reading up your this part of the code, I feel like there is some issues in collision detection by Raycast, seems like it's picking up some other unwanted colliders too. What I suggest is add a Layermask to Raycast and set that to check only for layer on which you have placed your baseTarget. The below mentioned documentation might help. link text
$$anonymous$$eep me informed. :) I will try to help the best I could..
Your answer
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
How can i rotate all the child objects together at the same time ? 1 Answer
How can i give another name/number to the created Plane object name ? 0 Answers
How can i check if the alpha color or the material color is transparent ? 1 Answer