• 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.

avatar image Spincu · Jul 18, 2013 at 05:12 PM 0
Share

can you provide a link ? Thank you.

avatar image Spincu · Jul 18, 2013 at 05:27 PM 0
Share
 public var myobject : GameObject;
  
 function Update () {
  
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         
          if (Physics.Raycast (ray, hit, 100)){
                 myobject.transform.position = hit.point
         }
 }

I tried this. myObject being the empty mouse object that the turret looks at

The movement is still very chaotic

avatar image robertbu · Jul 18, 2013 at 06:58 PM 0
Share

First replace the empty mouse object with a cube or a sphere you can see and verify that the object is being placed correctly. Without a video or something, I have no idea what you mean by 'chaotic'. You might try smoothing the movement of the mouse object:

 if (Physics.Raycast (ray, hit, 100)){
        myobject.transform.position = Vector3.Lerp(myObject.transform.position, hit.point, speed * Time.deltaTime);
 }
avatar image Spincu · Jul 18, 2013 at 08:25 PM 0
Share

https://www.dropbox.com/s/melf32dc7nhbdmm/desktop.unity3d

if you have unity player just load this and give it a try to see what I'm talking about :). It's really not that good. Plus if a put the mouse on the skybox it seems that the target won't go up, it just disappears into the z axis somehow :)

avatar image Spincu · Jul 18, 2013 at 09:13 PM 0
Share

Thank you for your help so far. I really hope I can get this over with.

avatar image robertbu · Jul 19, 2013 at 01:13 AM 0
Share

Some of the 'chaos' is due to the movement of the camera. What script are you using? As for the skybox, the issue is that there is nothing with a collider above the horizon. Here is a modification to your code to solve the problem:

 public var myobject : GameObject;
 
 function Update () {
 
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
 
          if (Physics.Raycast (ray, hit, 100)){
                myobject.transform.position = hit.point
         }
         else {
            myobject.transform.position = ray.GetPoint(some_dist);
         }
 }

Where 'some_dist' is a distance you define.

avatar image Spincu · Jul 19, 2013 at 05:37 AM 0
Share

I just attached the camera to the cannon rectangle so it follows where it's ai$$anonymous$$g at. Also how do I restrict the vertical angles of the cannon ?

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.

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

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

Launch a projectile from one object to another 1 Answer

Aiming Gun at Cursor 2 Answers

How can i set up a turret that turns and shoots where you touch onscreen?,how to shoot where you touch 2 Answers

Smoothing Moves issue. 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