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 wktk3 · Feb 10, 2014 at 12:33 PM · c#turret

How rotate turret and follow body object.

I want rotate my turret, but does not work.

This function called once per frame. And this 2 codes have same problem.

When i use Transform.rotation,turret don't follow my body.(sphere is testObject) alt text

And if i use Transform.localRotation then drive and turn my car, look like this. alt text

How fix this ? Please help ! and apologise my terrible English.

     void RotateTurret()
     {
         //turret
         Ray ray = _mainCameraTransform.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if( _terrainCollider.Raycast(ray,out hit,Mathf.Infinity) )
         {
             Vector3 targetPosition = hit.point;
     
 //code1
 //            Vector3 newRotation = Quaternion.LookRotation(targetPosition - _turretTransform.position).eulerAngles;
 //            Vector3 angles = _turretTransform.rotation.eulerAngles;
 //            _turretTransform.rotation = Quaternion.Euler(0,Mathf.SmoothDampAngle(angles.y,newRotation.y,ref _ref,1.0f, 40.0f),0);
 
 //code2
 //            Vector3 newRotation = Quaternion.LookRotation(targetPosition - _turretTransform.position).eulerAngles;
 //            newRotation.x = 0; newRotation.z=0;
 //            _turretTransform.rotation= Quaternion.Slerp(_turretTransform.rotation,Quaternion.Euler(newRotation),Time.deltaTime);
 
             //debug
             testObject.position = targetPosition;
         }
     }


01.jpg (109.7 kB)
2.jpg (125.7 kB)
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 1elfdragon1 · Feb 10, 2014 at 06:29 PM 0
Share

i'm not really sure what you're trying to make, but maybe this?

 RaycastHit hit;
 Physics.Raycast(the$$anonymous$$ainCamera.transform.position, the$$anonymous$$ainCamera.transform.forward, out hit);
 shootThing.transform.LookAt(hit.point);
avatar image wktk3 · Feb 11, 2014 at 06:26 AM 0
Share

Thanks reply but i don't want camera forward direction and raycast-hit.point.

i want rotate Yaw of _turretTransform with mouse.

That sphere just locate where hit.point for debug. Turret should be rotate that sphere direction. Sorry that may confuse you.

2 Replies

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

Answer by robertbu · Feb 11, 2014 at 06:34 AM

Turret rotation for a tank that can move over a terrain is complicated. Below is a script I wrote for another question, but the code was buried down some levels in comments, so I'll repost it here. It has a restriction on the angle of the gun. This restriction is based on the starting angle of the gun.

This code is designed to be added to the turret. Then the guns (as child objects) would initialize the 'gun' transform.

I don't know how you are handling your target, but in this script 'target' needs to be initialized.

 #pragma strict
 
 var target : Transform;
 var gun : Transform;
 var turretDegreesPerSecond : float = 45.0;
 var gunDegreesPerSecond : float = 45.0;
 
 var maxGunAngle = 45.0;
 
 private var qTurret : Quaternion;
 private var qGun : Quaternion;
 private var qGunStart : Quaternion;
 private var trans : Transform;
 
 function Start() {
     trans = transform; 
     qGunStart = gun.transform.localRotation;
 }
 
 function Update () {
     var distanceToPlane = Vector3.Dot(trans.up, target.position - trans.position);
     var planePoint = target.position - trans.up * distanceToPlane;
 
     qTurret = Quaternion.LookRotation(planePoint - trans.position, transform.up);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTurret, turretDegreesPerSecond * Time.deltaTime);
 
     var v3 = Vector3(0.0, distanceToPlane, (planePoint - transform.position).magnitude);
     qGun = Quaternion.LookRotation(v3);
 
     if (Quaternion.Angle(qGunStart, qGun) <= maxGunAngle)
        gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGun, gunDegreesPerSecond * Time.deltaTime);
     else
        Debug.Log("Target beyond gun range");
 }
Comment
Add comment · Show 5 · 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 wktk3 · Feb 11, 2014 at 09:30 AM 0
Share

Nice! This is exact im looking for few days! Thank you!

I copied in c#.

 void RotateTurret()
 {
     float _deltaTime = Time.deltaTime;
     Ray ray = _mainCameraTransform.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if( _terrainCollider.Raycast(ray,out hit,$$anonymous$$athf.Infinity) )
     {
         Vector3 targetPosition = hit.point;
         Vector3 turretPosition = _turretTransform.position;
         Vector3 turretUpTransform = _turretTransform.up;
         float distanceToPlane = Vector3.Dot(turretUpTransform,targetPosition - turretPosition);
         Vector3 planePoint = targetPosition - turretUpTransform * distanceToPlane;
         
         //turret
         Quaternion qTurret = Quaternion.LookRotation(planePoint - turretPosition,turretUpTransform);
         _turretTransform.rotation = Quaternion.RotateTowards(_turretTransform.rotation, qTurret,30.0f * _deltaTime);
         
         //gun
         Vector3 v = new Vector3(0,distanceToPlane,(planePoint - turretPosition).magnitude);
         Quaternion qGun = Quaternion.LookRotation(v);
         if(Quaternion.Angle(Quaternion.identity, qGun) <= 30.0f)
             _gunTransform.localRotation = Quaternion.RotateTowards(_gunTransform.localRotation,qGun,30.0f * _deltaTime);
         
         //debug
         testObject.position = targetPosition;
     }
 }
avatar image David_The_Boss · May 14, 2016 at 10:24 AM 0
Share

Hi I am trying to rotate the turret where mouse is pointing, your script is great, can you modify it so the turret would rotate where mouse is pointing ? Thanks!

avatar image Fressno David_The_Boss · May 16, 2016 at 09:17 PM 0
Share

hey bud. how did you make it work? im very interested.

avatar image David_The_Boss Fressno · May 17, 2016 at 11:54 AM 0
Share

Actually dont, it works only in front of the tank, transformed targets position into mouse position , input.mouseposition x and y, and some things proportionally with the screen width and lenght

avatar image meat5000 ♦ · May 14, 2016 at 10:44 AM 0
Share

Come back to us @robertbu

UA needs you :)

avatar image
0

Answer by sumeetkhobare · Feb 10, 2014 at 09:06 PM

Like 1elfdragon1 said , I am not sure too what you want exactly, but I think you want to ask your turret to look where your cursor is right?

I am assuming that your tank is rolling on a flat ground.

try this (I am writing the 'if' condition part only):


if(_your condition_)
{
   Vector3 targetPosition = hit.point;
   Vector3 newRotation = Quaternion.LookRotation(targetPosition - _turretTransform.position,_turretTransform.up).eulerAngles;
   _turretTransform.rotation= Quaternion.Slerp(_turretTransform.rotation,Quaternion.Euler(newRotation),Time.deltaTime);
}
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 wktk3 · Feb 11, 2014 at 07:53 AM 0
Share

Yes you are right. Your code is much better than $$anonymous$$e.

But turret still rotate x-axis (pitch) like 1st picture. How stop that?

Also i uploaded test build and full C# code on my WordPress. $$anonymous$$y WordPress log Can you try that?

avatar image wktk3 · Feb 11, 2014 at 09:46 AM 0
Share

I fixed problem with robertbu's script. Thank you for comment!

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

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

Related Questions

Automated turret targets around origin, regardless of location 1 Answer

Turret Script Error 2 Answers

Switching multiple cameras 0 Answers

Moving graph using vectrosity 1 Answer

Access to the path "...Temp\Assembly-CSharp-firstpass.dll.mdb" is denied? 4 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