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 /
avatar image
0
Question by FedaYkinG · Oct 19, 2016 at 10:45 PM · axisenemy ailockfollow playerair

How can I make a flying object follow ground player and stay airborne?

Basically I have a police helicopter that is airborne, and a player-controlled car on the ground. The helicopter would be supposed to follow the car but stay airborne, basically locking the Y axis. I don't care about collisions for this, so if Y axis is locked is fine by me, since i'd prefer to not give the helicopter collision. Below is the code. I'm aware that by using LookAt() and by adding forward it is going to move on all axis, so my question is how can I make it to move only on X and Z, with Y being locked in order to keep the helicopter airborne.

using UnityEngine; using System.Collections;

public class PoliceHelicopterController : MonoBehaviour {

 public float MinDistance = 35;
 public float MaxDistance = 10;
 public float Speed = 7;
 public Transform Player;

 void Update () {
     transform.LookAt(Player);

     if (Vector3.Distance(transform.position, Player.position) >= MinDistance)
     {
         transform.position += transform.forward * Speed * Time.deltaTime;
     }
 }

}

Thanks.

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

2 Replies

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

Answer by qjoman · Oct 20, 2016 at 01:16 PM

Use [Vector3.MoveTowards]. This will give you a position, moving from a given position (first parameter) to the given target (second parameter) using a step (third parameter).

I can't run the script right now, but I think it'll be something like this:

 public class PoliceHelicopterController : MonoBehaviour {
         public float MinDistance = 35;
         public float MaxDistance = 10;
         public float Speed = 7;
         public Transform Player;
     
     
         void Update () {
             transform.LookAt(Player);
             if (Vector3.Distance(transform.position, Player.position) >= MinDistance)
             {
                 Vector3 follow = Player.position;
                 //setting always the same Y position
                 follow.y = this.transform.position.y;

                 // remenber to use the new 'follow' position, not the Player.transform.position or else it'll move directly to the player
                 this.transform.position = Vector3.MoveTowards(this.transform.position, follow, Speed * Time.deltaTime);
             }
         }
     }
Comment
Add comment · Show 1 · 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 FedaYkinG · Oct 20, 2016 at 04:25 PM 0
Share

Just like 15 $$anonymous$$s ago I tried $$anonymous$$oveTowards and it worked! Thanks anyway, I'm sure your answer might help other people as well.

avatar image
0

Answer by Dune2 · Oct 19, 2016 at 11:18 PM

Use only the X & Z axis of the player in your following object update.

IE, something like

 var p = player.position;
 p.y = follow.position.y; // So follow keeps its altitude
 follow.position = p;
Comment
Add comment · Show 1 · 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 FedaYkinG · Oct 20, 2016 at 12:12 PM 0
Share

That works almost perfectly. The only issue is that the helicopter gets on top of the car instantly, since follow.position = p. What should I use to move to the player object based on the Speed property? So that it follows the player, based on X and Z, but does not instantly reach to it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating an object around Z axis using Lerp 2 Answers

Look At while locked in a position 1 Answer

Move player along mesh 0 Answers

Creating a multiple part turret what locks onto certain axis. 4 Answers

How to lock the y axis in camera script? 2 Answers


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