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 APProjects · Mar 19, 2013 at 06:37 PM · vector3anglemissilehoming

Angle between player and rocket targetting player.

I'm stuck with the problem mentioned in the question. I thought it's very easy, but I do something wrong. So I have a rocket which is following the player (edited from Smooth Follow script).

Now, this rocket is dodgeable by the player for a reason, but as it can't turn enough quickly to the player, it starts to set an orbit around the player and it goes around the player game object. The reason is pretty understandable, the rocket is too fast and it can't turn with that speed, it stucks around the player.

I want to make the player's situation more dangerous by slowing down the rocket, when it's angle relative to the player is too high. For example, if angle is larger than 30, slow down the rocket to follow the player easier, if it is less than 30 again, fly with maximum speed.

Of course, if the player is behind the rocket (rocket is facing the opposite direction), slow down to the minimum speed allowed for the rocket, find the target and again, fly with maximum speed.

What I was trying:

 angleToTarget = Vector3.Angle(target.position,transform.forward);

It's in Update() of course.

I guess, the problem is that transform.forward, it is applied in world space, not locally. I need a solution that works in local space. I hope it is understandable, I usually over-complicate things, but... nevermind!

A bad drawing about the problem: I just fail at rocket science...

rocketsciencefail.png (97.6 kB)
Comment
Add comment · Show 2
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 APProjects · Mar 19, 2013 at 07:10 PM 0
Share

I just read, transform.forward uses local axis... hmmm, then, how should I make it work?

avatar image whydoidoit · Mar 19, 2013 at 07:19 PM 0
Share

(See my answer?)

1 Reply

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

Answer by whydoidoit · Mar 19, 2013 at 06:43 PM

Ah you're nearly there - the problem is that you don't need to use the target position you need to use the target.position - transform.position to get the vector.

There's another choice though: you can use the Dot Product of the vectors so:

   var dot = Vector3.Dot((target.position - transform.position).normalized, transform.forward);

This will give you a value between -1 and 1. 1 when the rocket is facing the correct direction, 0 when it is at right angles and -1 when it is facing the wrong way. With a bit of clever clamping and manipulation or perhaps a Lerp you could easily slow down your rocket!

   var rocketSpeed = Mathf.Lerp(minimumRocketSpeed, maximumRocketSpeed, Mathf.Clamp01(dot));
Comment
Add comment · Show 3 · 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 APProjects · Mar 20, 2013 at 03:37 PM 0
Share

Now, I'm much closer to the solution! Thanks! One thing, that I wasn't able to do yet is the correct speed controlling. I tried to use an AnimationCurve to speed up and slow down my rockets, and the result is not bad, but far from the best.

What I want is if the player dodges the rocket, don't slow down immediately, but slow down slowly, so the rocket doesn't attack the player after the rocket passes next to it. So I want my rocket goes away, turn back and charge at the player. Hopefully, I can do that.

Anyway, is AnimationCurve O$$anonymous$$ in performance?

avatar image whydoidoit · Mar 20, 2013 at 03:47 PM 0
Share

Sure how about:

    float rocketSpeed;
 
    ...
 
    rocketSpeed = $$anonymous$$athf.Lerp(rocketSpeed, $$anonymous$$athf.Lerp($$anonymous$$imumRocketSpeed, maximumRocketSpeed, $$anonymous$$athf.Clamp01(dot)), Time.deltaTime);

This will lerp with damping the current rocketSpeed towards the ideal speed based on the current directions. You could also use SmoothDamp.

avatar image APProjects · Mar 22, 2013 at 12:46 PM 0
Share

Thanks, it is working well, just needs a little tweaking, and it'll be perfect!

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

11 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

Related Questions

C# - 2D - How can I make my homing rockets follow my player, and when my player dies, the rocket just keeps going forward? 1 Answer

Vector3.Angle() Never Reaching 0 1 Answer

Angle between vectors - calculation 2 Answers

Get Direction from 2 Vectors - and Apply to Transform 0 Answers

How do I find the angle between the players forward vector and the mouse position? 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