• 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 monsterjamp · Feb 14, 2013 at 01:25 AM · aiturretaimingcannon

Autoaiming Turret Cannon?

I'm trying to make a cannon that aim at the player. I can't seem to make it work properly. So I looked around the web and tried a few scripts but they all make my cannon rotate at odd angles. I'm trying to only make it rotate left-right and up-down (On the Z and X axis). Here is an image to a similar cannon to the one I made. alt text Also is there a way to specify the pivot point?

Comment
Add comment · Show 1
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 AlucardJay · Feb 14, 2013 at 01:25 AM 1
Share

http://cgcookie.com/unity/2012/05/22/unity-tower-defense-tutorial-part-01-autoturret/

3 Replies

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

Answer by monsterjamp · Mar 05, 2013 at 01:49 AM

http://forum.unity3d.com/threads/170466-Turret-Aiming

It was just my model was facing the wrong direction.

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
0

Answer by robertbu · Feb 14, 2013 at 04:17 AM

Take a look at this post:

http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html

Some changes need to be made if the gun is not at 0 on the Y axis. Plus, when shooting something, the ball is impacted by gravity, so the ball will drop with respect to the target.

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 monsterjamp · Feb 14, 2013 at 10:16 PM 0
Share

It works but it doesn't rotate right because the center of the turret looks at the target not the tip of it.

avatar image robertbu · Feb 15, 2013 at 12:07 AM 0
Share

The easiest way of handling this issue is to use an empty game object. Place the empty game object at the point you want it to pivot. It gets the script. The barrel of the gun is a child of the empty game object, and is translated so that the end of the barrel that pivots is at the empty game object. In addition, I usually put a empty game object just off the other end of the barrel to use as a spawn point for the shells.

avatar image
0

Answer by RyanZimmerman87 · Feb 14, 2013 at 04:32 AM

if you want it only to rotate on Z and X axis try something like this:

 Vector3 cannonOrientationVector;
 
 void Update()
 {
 
 cannonOrientationVector = new Vector3(playerObject.transform.position.x, this.transform.position.y, playerObject.transform.position.z);
 
 this.transform.LookAt(enemyOrientationVector);
 
 }

That should work just fine if attached to your cannon script, and if you have the playerObject gameObject available for the cannon script.

Oh, also make sure your cannon starts with a transform.position.y of 0, and cannot be altered anywhere else in the script;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Turret + Cannon Mouse movement 1 Answer

AI Turret Aiming 2 Answers

Turrets |Aiming don't work 1 Answer

Optimizing enemy AI and a question about speed differences between C# and Javascript 2 Answers

Rotating turret floats off tank in a sideways circle. Cannon floats out of turret when rotating up. 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