Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 raycosantana · Jul 15, 2015 at 09:22 AM · camerarotationturret

Align camera and turret aim

Hi

I have this tank game im working on but I have a little problem, I need to align the turret rotation with the camera so the tank shoots where the camera is pointing but only on the Y axis (Camera's X axis), I tried to do this by changing the turret's rotation but you cant change just the rotation of one axis without affecting the other two axes, even if you add turret's rotation back in the other two axes it doesnt look right...

Any idea how can I do this properly?

Here is the Camera code Im using:

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour {
     public GameObject Player = null;
     public Vector3 OffSet;
     public int CameraIndex = 0;
 
 
 
     private OnlineTankController _Controller;
     float distance = 10f;
 
     float xSpeed = 40f;
     float ySpeed = 120f;
     public float yMinLimit = -20.0f;
     public float yMaxLimit = 80.0f;
     private float x = 0f;
     private float y = 0f;
     // Use this for initialization
 
     void Awake(){
         Vector3 angles = transform.eulerAngles;
         x = angles.x;
         y = angles.y;
 
         if (GetComponent<Rigidbody>() != null){
             rigidbody.freezeRotation = true;
         }
     }
     void Start() {
         _Controller = Player.GetComponent<OnlineTankController>();
         transform.rotation = _Controller.transform.rotation;
 
     }
     // Update is called once per frame
     void LateUpdate () {
         switch (CameraIndex){
             case 0:
                 distance = 10;
                 break;
             case 1:
                 distance = 15;
                 break;
             case 2:
                 distance = 20;
                 break;
         }
 
         if (Input.GetButtonDown("Camera")) {
             CameraIndex += 1;
 
 
         }
 
         if (CameraIndex >= 3){
             CameraIndex = 0;
 
         } 
         
         
         if (Player != null){
 
 
             x += Input.GetAxis("TurretX") * xSpeed * 0.02f;
             y -= (float)(-Input.GetAxis("TurretY") * ySpeed * 0.02f);
 
             y = ClampAngle(y, yMinLimit, yMaxLimit);
 
             Quaternion rotation = Quaternion.Euler(y, x, 0);
             Quaternion Turretion = Quaternion.Euler(Player.transform.rotation.eulerAngles.x, x, Player.transform.rotation.eulerAngles.z);
             Vector3 position = rotation * new Vector3(0.0f, 2, -distance) + Player.transform.position;
 
             transform.rotation = rotation;
             //_Controller.Turret.transform.Rotate(0,Input.GetAxis("TurretX")*10,0);
             _Controller.Turret.transform.rotation = Turretion;
             transform.position = position;
             
         }
 
     }
     static float ClampAngle(float angle, float min,float max){
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
         return Mathf.Clamp(angle, min, max);
     }
 }

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 raycosantana · Jul 16, 2015 at 01:40 PM 0
Share

Any help with this?

avatar image rob-whitten · Jul 23, 2015 at 08:06 PM 0
Share

$$anonymous$$y tank setup is a little different then yours because I use the view of the camera through a remote firing system and my camera moves as the barrel elevates and it sounds like you just want the turret rotation part. Is there a reason why you can't attach the camera to the turret? If the camera was a child of the turret and then you align the initial view, when you rotate the turret transform you wouldn't have to any math camera would just rotate with the turret.

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Freeze Camera Rotation that is attached to the object in certain moments 0 Answers

How to freeze the rotation or the position on the camera transform? 1 Answer

how do i make first person character rotate left and right along with camera? 0 Answers

Override Oculus Orientation 5.3 1 Answer

FPS camera movement breaks GameObject 2 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