Hi, i’m trying to code a special projectile, and got stuck on a bit of rotation code. What happens so far is the game raycasts to get the point the player is looking at, creates a projectile, and sets the point to a variable in the projectile called ‘target’. The projectile is given a random initial velocity, and i’m attempting to make it curve toward the target position. I calculated the angular difference between the start and target transforms, but don’t know how to implement the rotation code for it. I was hoping it would be as easy as calculating the angular difference in the horizontal and vertical planes, then have it rotate closer toward the target rotation every update, but I’m not used to working in radial units like this, and don’t know how to accurately do this. Anyone have advice on how to start?
(Here’s what I have so far)
public class BlastJetScript : MonoBehaviour {
public Vector3 target;
float diffX;
float diffY;
float diffZ;
float horizontalAngleDiff;
float verticalAngleDiff;
bool horiP;
bool vertP;
void Start () {
transform.rotation = Random.rotation;
/** The difference between the two transforms, used to calculate the angular displacement **/
diffX = target.x - transform.position.x;
diffY = target.y - transform.position.y;
diffZ = target.z - transform.position.z;
/** The angular difference along the vertical plane and the horizontal plane, in radians **/
verticalAngleDiff = Mathf.Atan2(diffY, diffZ);
horizontalAngleDiff = Mathf.Atan2(diffX, diffZ);
/**These two check if the horizontal and vertical diffs are positive or negative, I was thinking I could use this to check which way for it to rotate**/
horiP = diffX > 0;
vertP = diffY > 0;
}
void FixedUpdate () {
//Not sure what to do next...
}
}