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;
}
}
}