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
1
Question by Jason Mathews · Mar 05, 2010 at 04:39 AM · physicsprojectilesteering

Lock On Projectile

So what I want to do is, create a projectile and fire it at a specific target. We are creating a 3rd Person RPG and spells and such fly at the target. As it is we have the targeting down but I am having problems getting the projectile to fire from "Player" to the "Target". For some reason i just can't think 3D and how to get the math to move from Player to Target.

Any help would be wonderful.

//test attack if (GUI.Button (Rect (50, 200, 25, 25), "Attack")){ var instantiatedProjectile : Rigidbody = Instantiate (projectile, PC.transform.position, PC.transform.rotation); instantiatedProjectile.velocity = PC.transform.TransformDirection(Vector3(0, 0, 0)); Physics.IgnoreCollision (instantiatedProjectile.collider, PC.transform.root.collider);

 targobject.GetComponent("Targetable").Life = targlife - 2;
 targobject.GetComponent("Targetable").targeted = true;

}

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

4 Replies

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

Answer by Jason Mathews · Mar 07, 2010 at 10:49 PM

Ok so after a bit of looking around and working I came across LookAt, which seemed to solve the issue. I mean the projectile fires straight, but needs to fire at the target when first done. Here's what I did

if (GUI.Button (Rect (50, 200, 50, 25), PC.GetComponent("Attributes").Spell)){
    if (target != "None"){
        PC.GetComponent("Attributes").SpellDamage = 2;
        PC.transform.LookAt(targobject.transform);
        var instantiatedProjectile : Rigidbody = Instantiate(projectile, PC.transform.position, PC.transform.rotation );
        Physics.IgnoreCollision( instantiatedProjectile.collider,PC.transform.root.collider );
        instantiatedProjectile.velocity = PC.transform.TransformDirection(Vector3(0,0,speed));
    }
}

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
avatar image
3

Answer by StephanK · Mar 05, 2010 at 09:20 AM

You can get the direction to your target by doing this:

Vector3 direction = target.transform.position - player.transform.position;

Then all you need to do is move the projectile in "direction".

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 Jason Mathews · Mar 05, 2010 at 03:33 PM 0
Share

Hmm still seams to fire towards the camera ins$$anonymous$$d... This was similar to what I first thought about.

avatar image StephanK · Mar 05, 2010 at 09:46 PM 0
Share

$$anonymous$$aybe then you should just change the sign of the direction...?

avatar image
1

Answer by cregox · Mar 05, 2010 at 06:45 PM

Well, @spree gave you the way. But since you didn't get it, allow me to try and guide you through this path.

I hope you're using javascript. And I'm assuming your only issue here is making the projectile move towards the target. Also assuming you'll use the following lines inside some Player's script.

private var targobject : GameObject; var projectileVelocity : float = 90.0;

function FireProjectile () { var instantiatedProjectile : Rigidbody = Instantiate (projectile, PC.transform.position, PC.transform.rotation); Physics.IgnoreCollision (instantiatedProjectile.collider, PC.transform.root.collider); var targobjectDirection : Vector3 = targobject.transform.position - transform.position; instantiatedProjectile.velocity = targobjectDirection * projectileVelocity;

 targobject.GetComponent("Targetable").Life = targlife - 2;
 targobject.GetComponent("Targetable").targeted = true;

}

targobject must be set somewhere else, inside some script somehow. I've set it as 90 because I guess bullets go at 900 m/s and that would be too fast on Unity.

projectileVelocity is an arbitrary number set on Inspector.

FireProjectile should be called just once per projectile.

It will fire pointed to the target, but it will not follow it. For that, you'd need to write a second script and run it on Update. It could be done on that same script and it would be some more work. Keep in mind that doing an auto-follow with physics, at other hand, will require a lot more of work, and is most likely not needed - there's nothing much physical about such a projectile following a target.

I haven't tested this, so it might need some adjustments, but that's the basic idea.

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 Jason Mathews · Mar 06, 2010 at 01:33 AM 0
Share

Perhaps adding an example of what is going on. http://www.meidigitaldesigns.com/DWTargeting.html

Basically if you click on the box it targets it, the targeting screen updates with the name and HP. If you hit attack then the bolt fire and the HP go down. Now its just getting the bolt from the player to the box.

But this does help me, still getting used to 3D space for calling scripts.

avatar image Jason Mathews · Mar 06, 2010 at 08:11 PM 0
Share

I was thinking about this, if we just set it to move forward on the Z then all we need to do is change the rotation to face the target, and that would accomplish what I kind of want here. The question then would be how do we find the rotation that would put the Z facing our object.

avatar image cregox · Mar 12, 2010 at 02:39 PM 0
Share

@Jason sorry I somehow missed your comments - I wasn't notified about them... Dam I hate StackExchange notification system so much! Well, I'm glad to know this helped you. You should just accept your own answer if it solved your question. ;)

avatar image
1

Answer by Ricardo · Mar 07, 2010 at 11:07 PM

Or you could just use the UnitySteer functions, particularly steerForPursuit, exemplified on the MpPursuer vehicle and its corresponding PursuerBehavior. Depending on your needs, it's possible that said behavior will require only minimal modification.

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

No one has followed this question yet.

Related Questions

How do I arc a rigidbody so it lands on a target? 2 Answers

Cannon shoots in wrong direction with unrealisic Physic 1 Answer

Giving Instantiated Object Velocity Causes Wrong Position 1 Answer

How to predict where the ball will fall? 4 Answers

2D 360 degress platformer example needed 0 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