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 Yoshinator2 · Jul 24, 2017 at 08:04 PM · rotationquaternionmathglobalcalculation

Quaternion using global values instead of local. Please help :(

Hi, I am trying to have the turret rotate to where it should for the bullet to be fired correctly at the target. In this script I have it calculate what angle the bullet should be fired at. That part works perfectly. However, whenever this rotates the turret, it seems to use the global rotation instead of the local. Please note this script is attached to a parent object of the turret being rotated. What am I doing wrong? I used Debug.Log to determine that the turretRot variable is giving 270 degrees extra in the Y value because of the global rotation. I really am struggling with this and have been for a while, I would be thankful for any help at all. Thank you all. :)

EDIT: Would it just be easier (and less CPU intensive) to use a Bezier curve with different points, and have the bullet use a simple MoveTowards function to go to each point? I already have this set up I just thought this way that I am trying would look better and be more optimized. Is this true?

 public class AngleCalculator : MonoBehaviour {

 [SerializeField]
 Transform cannonBase;

 [SerializeField]
 Transform turret;

 [SerializeField]
 Transform firePoint;

 [SerializeField]
 GameObject projectilePrefab;

 [SerializeField]
 float cooldown = 1;

 private float currentSpeed;
 private float currentAngle;

 private float lastShotTime;

 public float turnSpeed;

 public float shootDelay;
 private bool isTarget;
 private Quaternion lookRotation;
 public Transform currentTarget;
 private float turretAng;

 public void SetTargetWithSpeed(Vector3 point, float speed, bool useLowAngle)
 {
     currentSpeed = speed;

     Vector3 direction = point - firePoint.position;
     float yOffset = direction.y;
     direction = direction - (Vector3.Dot(direction, Vector3.up) * Vector3.up);
     float distance = direction.magnitude;

     float angle0, angle1;
     bool targetInRange = LaunchAngle(speed, distance, yOffset, Physics.gravity.magnitude, out angle0, out angle1);

     if (targetInRange)
         currentAngle = angle0;
     //currentAngle = useLowAngle ? angle1 : angle0;

     SetTurret(direction, currentAngle * Mathf.Rad2Deg);
 }

 public void Fire()
 {

         GameObject p = Instantiate(projectilePrefab, firePoint.position, Quaternion.identity);
         p.GetComponent<Rigidbody>().velocity = turret.forward * currentSpeed;
         lastShotTime = Time.time;

 }
 //
 private void SetTurret(Vector3 dir, float turretAngle)
 {
     isTarget = true;
     lookRotation = Quaternion.LookRotation(dir);
     turretAng = turretAngle;
 }


 // Update is called once per frame
 void FixedUpdate () {
     //SetTargetWithSpeed(target.transform.position, initialFireSpeed, useLowAngle);
     if(isTarget == true)
     {
         Vector3 turretRot = new Vector3(-turretAng, lookRotation.eulerAngles.y, lookRotation.eulerAngles.z);
         Debug.Log(turretRot);
         Vector3 rotation = Vector3.MoveTowards(cannonBase.rotation.eulerAngles, lookRotation.eulerAngles, Time.deltaTime * turnSpeed);
         Vector3 rotation2 = Vector3.MoveTowards(turret.rotation.eulerAngles, turretRot, Time.deltaTime * turnSpeed);
         cannonBase.rotation = Quaternion.Euler(0f, rotation.y, 0f);
         turret.rotation = Quaternion.Euler(rotation2.x, rotation2.y, 0f);
     }
     else if(currentTarget == null && !isTarget)
     {
         isTarget = false;
     }
 }
 public void CalculateAngle(Transform target, float initialFireSpeed, bool useLowAngle)
 {
     SetTargetWithSpeed(target.position, initialFireSpeed, useLowAngle);
     currentTarget = target;
     StartCoroutine(WaitFire());
 }
 public IEnumerator WaitFire()
 {
     yield return new WaitForSeconds(shootDelay);
     Fire();
 }
 /// <summary>
 /// Calculates the two possible initial angles that could be used to fire a projectile at the supplied
 /// speed to travel the desired distance
 /// </summary>
 /// <param name="speed">Initial speed of the projectile</param>
 /// <param name="distance">Distance along the horizontal axis the projectile will travel</param>
 /// <param name="yOffset">Elevation of the target with respect to the initial fire position</param>
 /// <param name="gravity">Downward acceleration in m/s^2</param>
 /// <param name="angle0"></param>
 /// <param name="angle1"></param>
 /// <returns>False if the target is out of range</returns>
 private bool LaunchAngle(float speed, float distance, float yOffset, float gravity, out float angle0, out float angle1)
 {
     angle0 = angle1 = 0;

     float speedSquared = speed * speed;

     float operandA = Mathf.Pow(speed, 4);
     float operandB = gravity * (gravity * (distance * distance) + (2 * yOffset * speedSquared));

     // Target is not in range
     if (operandB > operandA)
         return false;

     float root = Mathf.Sqrt(operandA - operandB);

     //High Angle
     angle0 = Mathf.Atan((speedSquared + root) / (gravity * distance));
     //Low Angle
     angle1 = Mathf.Atan((speedSquared - root) / (gravity * distance));

     return true;
 }

}

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

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

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

Related Questions

Smooth rotation to child object 1 Answer

Quaternions local/world 1 Answer

Flight Movement with Banking 2 Answers

Trouble with Camera Rotation Math 2 Answers

Random.rotationUniform clarification 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