Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PositiveAnion · Aug 16, 2017 at 01:09 AM · rotationaxisturret

How to rotate cannon by angles on top of a turret

Hi! I am trying to get a turret for a flying battleship game to work, but I can't for the life of my get the elevation controls to work!

I have a 3 axis turret consisting of 2 pieces: A Turret, which turns horizontally on the Y axis, and a Cannon, which I would like to turn on the x axis. The cannon is set as a child of the turret.

The Turret's rotation is done by a scrip that points it towards a public gameobject, which is currently set as a point attached to my camera, which is on a custom mouse based 3rd person controller. It uses a script I canalized from a Quill18 script, and uses quaterions. It works wonderfully

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class TurretRotator : MonoBehaviour {

 public float rotSpeed = 90f;
 public float correctionAngle;
 public float maxright;
 public float maxleft;
 public GameObject aimpoint;

 //tracks the Aimpoint, as aimed by mouse
 public void Update()
 {
     Vector3 aim = aimpoint.transform.position;

     Vector3 dir = aim - transform.position;
     dir.Normalize();

     float yAngle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg + correctionAngle;

     //Debug.Log(yAngle);

     Quaternion desiredRot = Quaternion.Euler(0, yAngle, 0);

     transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRot, rotSpeed * Time.deltaTime);

 }

}

I must admit I am not that familiar with Unity's scripting, especially rotations, as I do not understand Quaternions very well, or how to manipulate them.

However, I am really struggling to get my turret to elevate. I have spent 2 days straight trying to get it to elevate by the angle, and cannot come up with a working script.

What I want to be able to to is to -send it data or get data from another script, allowing both the player to manually control it as well as AIs to control it -Rotate on the X axis to a certain degree, preferably a float. This will allow precise firing angles based on enemy ship's movement, distance, and elevation, all calculated by some other script. -keep the same base rotation as the Parent object, so it is facing the same direction as the turret is -Rotate over time, instead of just instantly snapping to the desired elevation -have a modifier that allows a certain angle to be set as the "zero" angle for cannon, this is important since my cannon's x rotation is set as -90 to make it face the horizon, otherwise it would point straight up.

So far, I have attempted to use many methods. These have had varying levels of success, from- -A simple script that elevated up and down with button inputs- press the button, if statement allows transform.Rotate to cause upwards or downwards rotation. Cannon stays with Parent, and keeps parents other rotations, only it's X axis is modified. completely imprecise, could not modify to get to hover on certain angles. any attempt to make this turn towards a certain angle and stop, caused constant rotation on the X axis(just spinning around). -A script attempting to turn Desired Angle into a Vector3, and feed it into a mechanism like the one used in the Rotation script, converting Vector3 into a Quaternion, and using a transform.rotation modifier to elevate cannon. This usually disconnected the cannon's rotation from the turret, and often simply did not elevate the turret. often times it made my turret point at it's natural "zero" x rotation, straight up, even with my attempts to correct this.

I am completely stumped here, and help I can get would be appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PositiveAnion · Aug 16, 2017 at 02:25 AM

AH-HA! I figured it out! Here is the script I made. I found that referencing the objects exact x rotation was both difficult and unnecessary! By simply forgoing this, I found I can just elevate by degrees/frame (currently .1 and can get accurate elevations! Plus, I can access my function from other scripts so I can make a "targeting computer later on!

I am sure I will need to adjust my numbers a bit, but for now, this should work well.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class TurretElevator : MonoBehaviour {

 public float elvSpeed = 10f;     //not currently used, will by max elevation rate, If i chose to limit (up for debate, adds unneeded complexity
 public float maxElevation = 40;  //upper elevation bound
 public float depression = 5;     //lower elevation bound
 private float elevation;         //holds the current elevation, in degrees
 private float elv;               //holds input for Update


 //this update includes a simple, tenth of a degree elevator/depressor.
 public void Update()
 {
     if (Input.GetButtonDown("Elevate"))
     {
         elv += .1f;
     }
     if (Input.GetButtonDown("Depress"))
     {
         elv -= .1f;
     }

     Elevate(elv);
 }

 // Update is called once per frame
 public void Elevate(float desiredElv)
 {     
     //if statement elevates, and should keep within the elevatin/depression limits
     if (elevation < desiredElv && elevation < maxElevation)
     {
         transform.Rotate(0.1f, 0, 0);
         elevation += 0.1f;
     }
     else if (elevation > desiredElv && elevation > -depression)
     {
         transform.Rotate(-0.1f, 0, 0);
         elevation += 0.1f;
     }
     else
         transform.Rotate(0f, 0, 0);
 }

}

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

89 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 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 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 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

Turret Rotation on a moving object with rotation limits 3 Answers

Constraining turret rotation in local space 1 Answer

Input.acceleration Y and Z are the same? 1 Answer

different axis for different objects? 1 Answer

Snap the rotation of object 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