hi all~
I have 2 gameobjects in my test scene, I called it origin and target.
and missile would fire from origin’s position and move towards target position in an arc .
I got the moving in arc motion done and is working…however I have trouble making missile rotates to the direction it is traveling.
I tried lookat , and Quaternion.LookRotation.But the same thing happens, it would not rotate pass 90 degree in X axis…I am not sure how to fix it…here is my code…please help…much appreciated.
using UnityEngine;
using System.Collections;
public class ds : MonoBehaviour {
public Transform origin;
public Transform target;
public Transform missile;
float distance;
Transform aobj;
float aval;
float roX;
float starttime;
bool canmove;
// Use this for initialization
void Start () {
distance = Vector3.Distance(origin.position,target.position);
Vector3 tpos = (origin.position + target.position) / 2f;
aobj = Instantiate(missile)as Transform;
aobj.position = origin.position;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonUp(0)){
canmove = true;
starttime = Time.time;
}
if(canmove){
Vector3 center = (origin.position + target.position) *0.5f;
center -= new Vector3(0,1,0);
Vector3 origincenter = origin.position - center;
Vector3 targetcenter = target.position - center;
float speed = (Time.time - starttime) / 5f;
Vector3 newpos = Vector3.Slerp(origincenter,targetcenter,speed);
//missile rotation here
Vector3 relativepos = target.position - aobj.position;
aobj.rotation = Quaternion.LookRotation(relativepos);
//missile movement
aobj.position = newpos;
aobj.position += center;
}
}
}