Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 EmpI · Jul 18, 2014 at 05:27 PM · projectileplanehoming

Lock Homing Missile to Plane (2D)

I am trying to make a missile that follows the player, one that rotates towards the direction of the player and generally acts as a homing missile does. The homing code works fine. However, it's a 2D game and I find my missile doing loop-de-loops onto the Z-axis when trying to find my player even though I want the missile to be bound to the X-Y plane.

Here is the code i'm using for the homing enemy (javascript)

 var target : Transform; 
 var moveSpeed = 4;
 var rotationSpeed = 1;
 
 var life = 0;
 
 function Awake()
 {
 }
 
 function Start()
 {
 target = GameObject.FindWithTag("Player").transform;
 transform.rotation = Quaternion.LookRotation(target.position - transform.position);
 }
 
 function FixedUpdate()
 {
 var lookDir = target.position - transform.position;
 //lookDir.z = 0;
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime);
 transform.position += transform.forward * moveSpeed * Time.deltaTime;
 }
 
 function Update()
 {
 life = life + 1;
 if (life > 300)
 {
 Destroy(gameObject);
 }
 }

you'll notice the lookDir.z = 0 code commented out. This was my attempt at fixing it, however the code caused odd things to occur.

Again, homing itself works fine, but how do I lock the missile to the x-y plane, preventing them from moving in the z-direction at all?

Comment
Add comment · Show 4
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 robertbu · Jul 18, 2014 at 05:27 PM 0
Share

You need to explain how the missile was a constructed. In particular, you need to explain what side of the missile is considered forward.

avatar image EmpI · Jul 18, 2014 at 05:38 PM 0
Share

The Forward side is always rotating or trying to rotate to face the player.

avatar image Kyuu · Dec 22, 2014 at 02:02 PM 0
Share

Have you ever found a solution to this? I'm having the same problem, having a hard time solving it.

avatar image EmpI · Dec 22, 2014 at 07:49 PM 0
Share

Yes. Your missile should have a rigidbody2D

 public GameObject player; //your missile's target
 public float speed = 1;

 private Vector2 dist = new Vector2();

     void Update () {
         dist = player.transform.position - transform.position; //difference in space between target and player
         dist = dist.normalized; //makes sure it's based on direction. Without it, the missile slows down the closer the missile is to target.
         rigidbody2D.AddForce (dist * speed);
 }

Play with rigidbody2D settings like mass, drag, and angular drag to get the effect you want.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hav_ngs_ru · Dec 22, 2014 at 02:55 PM

I guess, the Missile and Player z-coords are not exactly the same. In this case missile should twitch of cause.

Try to set transform.positino.z = target.transform.position.z in Start (before calculate rotation). And in FixedUpdate too (to avoid changing of z because of float round operations).

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotation troubles 2 Answers

Calculate angle between two locations, and then rotate object to face target based on this. 1 Answer

How do I create a solid GameObject (arrow) made out of multiple child objects (head, shaft, fletching)? 0 Answers

Arcing projectile using physics 0 Answers

Projectile Hit Detection with Bullet Drop 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