Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by haimmoshe · Jun 23, 2017 at 04:20 AM · c#scripting problemscript.

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.

Spaceship land in blender

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:

My spaceship landing test

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chocolade · Jun 24, 2017 at 07:44 AM 0
Share

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:

  1. 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.

  2. 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.

avatar image Manpreet_96 Chocolade · Jun 24, 2017 at 02:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

347 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges