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 Spincu · Jul 18, 2013 at 03:32 PM · shootturretaimingcannon

Turret + Cannon Mouse movement

Hey guys, this is a problem I've been strugling for days now but I can't seem to find a proper solution that will help me.

I've looked on other questions and formus but still I haven't find exactly what I need. I need two scripts: one for a turret that can spin around 360 degrees following the Mouse's X position and to stop when the X positions are the same. And I also need another script to rotate the cannon that's attached to the turret, but this has to spin only verticaly and between limits so it doesn't rotate into itself.

I am trying to make like a world of tanks playing style, this is what I'm trying to achieve:

http://www.youtube.com/watch?v=x__yN6hwuvI

Also I don't want any rigid bodies, nor do I want to use the Character Controller package in unity because it uses to much resources.

So far I came up with this:

Gun Script:

 var goTarget : GameObject;
 var maxDegreesPerSecond : float = 60.0;
 private var qTo : Quaternion;
  
 function Start () {
     qTo = goTarget.transform.localRotation;
 }
  
 function Update () {
     var v3T = goTarget.transform.position - transform.position;
     var v3Aim : Vector3;
     v3Aim.x = 0.0;
     v3Aim.y = v3T.y;
     v3T.y = 0.0;
     v3Aim.z = v3T.magnitude;
     qTo = Quaternion.LookRotation(v3Aim, Vector3.up);
     transform.localRotation = Quaternion.RotateTowards(transform.localRotation, qTo, maxDegreesPerSecond * Time.deltaTime);
 }


Turret Script:

 var goTarget : GameObject;
 var maxDegreesPerSecond : float = 30.0;
 private var qTo : Quaternion;
  
 function Start () {
     qTo = goTarget.transform.localRotation;
 }
  
 function Update () {
     var v3T = goTarget.transform.position - transform.position;
     v3T.y = transform.position.y;
     qTo = Quaternion.LookRotation(v3T, Vector3.up);       
     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);

}


But these follow an object in the game instead of the mouse position. I've also looked and ScreenToWorldPoint, Transform.Lookat and Raycasts but It just messes up everything even worse.

Please help me, I would really appreciate it.

Comment
Add comment · Show 12
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, 2013 at 04:08 PM 1
Share

You didn't write this code. I wrote this code. When borrowing code, please make reference to the original question and the original author.

avatar image Spincu · Jul 18, 2013 at 04:11 PM 0
Share

Sorry man, I didn't say I wrote it. It's what I came up with so far that's close to what I need. I wrote a tone of code trying to use the mouse but eventually messed everything up, that's why I posted this because it's the closest to what I needed.

avatar image robertbu · Jul 18, 2013 at 04:17 PM 0
Share

Use an empty game object for the target. Put code on the target that does a raycast each frame and places the empty game object at RaycastHit.point. If the raycast fails, don't move the target (so the gun will still point at the last position).

avatar image Spincu · Jul 18, 2013 at 04:22 PM 0
Share

I've tried that, but when the tank moves it just gets messed up. I've created an empty game object, attached it to the front of the cannon and used that but when the tank stars moving it gets messed up. Also I need to constraint the vertical movement somehow, and I would like the turret to rotate untill it catches up with the mouses's X. This one just rotates it while the mouse is moving, when i stop moving the mouse it slow down and never catches up with the mouse position.

avatar image robertbu · Jul 18, 2013 at 05:02 PM 0
Share

Forget the turret code for a $$anonymous$$ute. Just write a script that uses Raycasting() to place a object at the mouse cursor position. The question of placing an object at the mouse cursor using Raycasting has been answered many times on Unity Answers. After you have that working, just drag and drop the object into the goTarget variable in the Inspector.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Spincu · Jul 22, 2013 at 05:03 PM

How do I limit the rotation of the gun in this script ?

 //public variables
 public var targetFollow        : GameObject;             //load the empty game object that the cannon must follow on y axis
 public var maxDegreesPerSecond : float = 60.0;           //rotation speed of cannon
 
 //private variables
 private var qTo : Quaternion;
 
 //load the followed object current rotation
 function Start () {
     qTo = targetFollow.transform.localRotation;
 }
 
 // y axis movement to follow target
 function Update () {
 
      var v3T = targetFollow.transform.position - transform.position;
      var v3Aim : Vector3;
        
        v3Aim.x = 0.0;
        v3Aim.y = v3T.y;
        v3T.y = 0.0;
        v3Aim.z = v3T.magnitude;
   
           qTo = Quaternion.LookRotation(v3Aim, Vector3.up);
           transform.localRotation = Quaternion.RotateTowards(transform.localRotation, qTo, maxDegreesPerSecond * Time.deltaTime);
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

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

Related Questions

Autoaiming Turret Cannon? 3 Answers

making an object stop responding with an object at a certain distance away 1 Answer

first Gun fires not well 2 Answers

Shooter in iOS and a visible Aim line before shooting 0 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