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 Toni95 · Jun 08, 2015 at 09:19 AM · positionshootaimspecific

Instantiate a projectile facing a Vector3 position in C#

Hello Unity community.

I have a script that allows a turret to look and shoot a target, but not to its current position, to an intercept position so that the turret will hit the target if the target doesn't change its speed and direction, but my problem is that I don't know how to instantiate the projectile to face this Vector3 position. Any ideas?

PD: The projectile script allows projectiles to go forward with x speed, and it doesn't use gravity.

Thanks.

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 Nerevar · Jun 08, 2015 at 09:27 AM 0
Share

maybe you can use LookAt() (creating a temporay transform with that Vector3 position)? or you can use the same methods you have for your turret? or even copy the turret transform value (rotation) to you projectile.

If you are able to manipulate your turret as you said, you should not be stuck on this projectile orientation.

avatar image Toni95 · Jun 08, 2015 at 11:50 AM 0
Share

But I didn't want to shoot to the direction the turret was facing, I wanted it to go to the target relative position, you know, "leading shoot", so it would hit the target even in movement.

Well, I'm pretty new in Unity.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Jun 08, 2015 at 09:29 AM

Quaternion.LookRotation gives you a rotation that looks in a certain direction:

 Quaternion.LookRotation(new Vector3(0, 1, 0)); //rotation that looks straight up

To get a vector that points from point a to point b, do b - a:

 Vector3 a = new Vector3(1, 1, 1);
 Vector3 b = new Vector3(5, 1, 1);
 Vector3 aTob = b - a; // gives (4, 0, 0);

Combine those to get the correct instantiation:

 Vector3 spawnPoint = turret.transform.position;
 Vector3 targetPoint = target.transform.position;

 Vector3 toTarget = targetPoint - spawnPoint;

 Instantiate(bulletPrefab, spawnPoint, Quaternion.LookRotation(toTarget));

Good luck!

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 Toni95 · Jun 08, 2015 at 11:46 AM 0
Share

Thank you for answering, I just noticed what I was doing wrong... I wasn't substracting the shotSpawn.position, so it was shooting to a crazy position. Well, I have another problem now, the projectile is just facing the current target position, not the relative target position. I copied a code that calculates this relative position here: http://wiki.unity3d.com/index.php/Calculating_$$anonymous$$_For_Projectiles Apparently it isn't working for me, do you know if it works? Thank you again.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Shoot second cell in 2d 0 Answers

Orthographic position to Perspective position 0 Answers

Aiming problem 2 Answers

Bullet Positioning 2 Answers

Aim to crosshair? 3 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