• 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 /
  • Help Room /
avatar image
Question by soccer1mt · Oct 08, 2016 at 09:49 PM · rotationlerprotation axisslerpmissile

How to control rocket pathing with Quaternion.Slerp

So I currently have code that shoots a rocket from the player location to a target position. The intention is to have a nice straight arc where the rocket goes up and then arcs and comes down. When the player is shooting the rocket in the +z direction, the path for the rocket is fine. When I shot in the -Z direction or from side the side, the rocket starts to take really weird paths (i.e. sweeps to the sides really far ) and doesnt have the trajectory I am looking for. I am using Quaternion.Slerp and don't really understand how it works. I've tried looking in to using a bezier curve implementation but it just doesnt have the missile shooting feel that I am looking for .

Question is...is there a way to contain a Quaternion.Slerp to a plane ( I'd like the rocket to travel in an arc along a plane that is straight up and down and passes through the player and target points.

I use pretty standard missile homing code in the missileTracking() function. Any suggestions or recommendations for how to get the intended behavior would be much appreciated. TYTY

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using VRTK;
 
 // try moving along bezier curve that creates a middle control point using the plane equation.
 public class MissileMove : MonoBehaviour {
 
     private int j;
     [HideInInspector] public Vector3 target;
     private Rigidbody missileRB;
 
     private float deploymentTime;
     private float deployHeight =.005f;
     private float deploySpeed =3f;
     private float turnTime;
     private float velocity=2;
     private float torque;
 
     List<Transform> missileChildren = new List<Transform>();
     List<Vector3> childrenAngles = new List<Vector3>();
     private Vector3 targetAngle = new Vector3(-90f, 0f, 0f);
     
 
     private bool missileFiring = false;
 
 
     void OnEnable()
     {
     
          j = Random.Range(0, 4);
         missileRB = GetComponent<Rigidbody>();
     }
 
 
 
     // Use this for initialization
     void Start ()
     {
         deploymentTime = Time.time;
         torque = torqueGetter();
         int children = transform.childCount;
         for (int i = 0; i < children; ++i)
         {
             missileChildren.Add(transform.GetChild(i));
             childrenAngles.Add(transform.eulerAngles);
 
         }
 
     }
 
     void OnDisable()
     {
         this.gameObject.GetComponent<missileExplosion>().enabled = true;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (!missileFiring)
         {
  
             foreach(Transform child in missileChildren)
             {          
                 child.transform.rotation = Quaternion.Slerp(child.transform.rotation, Quaternion.Euler(new Vector3(-90f,0f,0f)), ((Time.time - deploymentTime)/5.0f)-.25f);
  
             }
             
         }
         if (!missileFiring)
         {
             StartCoroutine(MissileDeploy());
 
         }
         if (missileFiring)
         {
             MissileTracking();
 
             foreach(Transform child in missileChildren)
             {
                 child.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
             }
         }
     }
 
 
     IEnumerator MissileDeploy()
     {
 
         if (Time.time - deploymentTime < 1.2)
         {
             transform.Translate(Vector3.forward * deployHeight * Mathf.Sin(deploySpeed * (Time.time - deploymentTime)));
         }
         if(Time.time-deploymentTime>1.2)
         {
             missileRB.velocity = Vector3.down* .08f;
             yield return new WaitForSeconds(1.5f);
             missileFiring = true;
             missileRB.GetComponent<Collider>().enabled = true;
         }
     }
 
     void MissileTracking()
     {
         turnTime += (Time.deltaTime * (1/torque));
        if (target == null || missileRB == null)
        {
            return;
        }
        velocity += Time.deltaTime;
        missileRB.velocity =.5f* velocity*velocity * missileRB.transform.forward;
        Vector3 lookPos = target - transform.position;
        Quaternion rotation = Quaternion.LookRotation(lookPos);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation,  turnTime);
         
     }
 
     float torqueGetter()
     {
 
         float dist = Vector3.Distance(target, transform.position);
         if (dist > 1f)
         {
             return dist * 5f;
         }
         else
         {
             return dist * dist * 5f;
         }
     }
 
 
 
 void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.CompareTag("Floor"))
         {
             Vector3 position = this.gameObject.transform.position;
             position.y = .65f;
             this.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
             this.gameObject.transform.position = position;
             this.gameObject.GetComponent<missileExplosion>().enabled = true;
         }
     
     }
 }
 
 
Comment

People who like this

0 Show 0
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

Answer by conman79 · Oct 08, 2016 at 10:38 PM

The problem is because you're rotating to the world space rotation new Vector3(-90f,0f,0f), when you only want to rotate on the local x axis.

On line 65, to isolate the rotation to the x axis, change your code like this::

 child.transform.rotation = Quaternion.Slerp(child.transform.rotation, Quaternion.Euler(-90f, transform.localEulerAngles.y, transform.localEulerAngles.z), ((Time.time - deploymentTime)/5.0f)-.25f);




Comment

People who like this

0 Show 3 · 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 soccer1mt · Oct 08, 2016 at 10:47 PM 0
Share

That is actually not the part of the code that is giving me trouble. The part of the code you are referring to controls the wings on the missile coming down after the rocket deploys (it was a work around because I couldn't figure out the animator). It is working as I intended.

The code that is giving me trouble is in lines 103-114. Thank you!

avatar image conman79 soccer1mt · Oct 08, 2016 at 11:33 PM 0
Share

I can't see anything wrong with the rotation code in lines 112-114, I even tested it in a quick Unity project and it worked fine. There's probably something somewhere else in the code causing issues but I can't figure it out right now. The problem screams local vs. world rotations/positions though.

If you could upload a sample project somewhere I could take a look.

avatar image soccer1mt · Oct 08, 2016 at 11:14 PM 0
Share

With that being said, your suggestion solved another problem that I was having ( namely the one that you were addressing) in a much more elegant way than the way that I was solving it. Makes it look cleaner and better. Thank you!

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

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

Rotate on Transform.localeulerAngels smoothly 0 Answers

How to move Tile Up, Rotate by 90 degrees, then down again on click, while supporting dragging tile freely 2 Answers

Lerp going crazy when negative 1 Answer

Rotation (Quaternion.Lerp) not working 0 Answers

How to Slerp / Lerp Rotation on a Single Axis? 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