- Home /
Make a side of an object LookAT another object
Basicly I have 2 ships, one is controlled by the player and the other by the computer.
I have a script where the AI Ship rotates towards the player and starts moving towards him. But, once within firing range, it needs to rotate around 90 degrees left or right so that the cannons on either side can have a shot and that is where i've been having difficulty.
I've tried multiple methods but none have worked, seems that i'll never understand Quaternions.
else if(Vector3.Distance(transform.position, player.position) < firingRange && Vector3.Distance(transform.position, player.position) > retreatDistance) //If in firing range
{
//stop moving
transform.position = this.transform.position;
//rotate & fire
Vector3 fireAngle = transform.rotation * new Vector3(0,90,0); //add 90 degrees on Yaxis
Quaternion rotation = Quaternion.LookRotation(fireAngle);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed + Time.deltaTime);
}
Any help would be greatly appreciated as I've been stuck in this part for way too long
Answer by ShadyProductions · Apr 18, 2019 at 12:54 PM
Hello,
You can grab the direction of the target, and then set the side of your ai ship to point at the direction.
var direction = (target.transform.position - aiShip.transform.position).normalized;
// Set the right side to point at the direction
aiShip.right = direction; // You could slerp this to make the transition more smooth instead of instant
You'll have to rotate to left or right based on which side is the fastest to turn towards the target. I assume you could use Dot product for this.
Thank you so much for your help @ShadyProductions!
It worked almost as intended, like you mentioned, I need to Lerp the last statement for a smoother transition.But Quaternions are my weakness and i could never figure them out for life of me (Always sucked at $$anonymous$$ath). I've been trying to get the Lerp working, this is what i got so far:
Vector3 fireDirection = (player.transform.position - transform.position).normalized;
Quaternion rotation = Quaternion.LookRotation(firedirection);
transform.right = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
But as you can probably tell I get an error, can you please tell me what I'm doing wrong and if you could explain it in more detail for my sake and for everyone else following this question!
$$anonymous$$uch appreciated!
@Saif_youssef89 You can use slerp to spherically interpolate, this treats the vectors as directions rather than points in space like lerp does. This should work:
var direction = (Target.transform.position - transform.position).normalized;
transform.right = Vector3.Slerp(transform.right, direction, Time.deltaTime);
Sorry for the late reply.
Your answer
Follow this Question
Related Questions
How can I multiply the rotation of a mirrored object? 1 Answer
how to convert the angle to roatation? 1 Answer
Unity Rotate Raycast on Quaternion 1 Answer
Problem with Quaternion.Euler() when rotating object 3 Answers
Perpendicular Vector3 0 Answers