EARTH DEFENDER QUEST: ‘Rotate the Towers’ - Solutions

Quest: Earth Defender Quest
Challenge: Rotate the Towers

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

I went with this to rotate the towers

var newDirection = Vector3.RotateTowards(mylaser.transform.forward, target.transform.position - mylaser.transform.position, Time.deltaTime * missileSpeed,0f);

mylaser.transform.rotation = Quaternion.LookRotation(newDirection);

This one took a bit of thinking. I’m not sure that my solution is the cleverest or the best one, but basically all I did was reorient the individual pieces of the tower prefab, so they were aligned with the z axis (what unity uses as the “forward vector”). Then, I just simply set my instantiated tower to rotate and look at the planet. Now, all my towers correctly rotate away from the planet when they are placed.

Here’s the solution I came to:

Vector3 hitPoint = hit.point - hit.collider.transform.position;
Quaternion towerRotation = Quaternion.FromToRotation(Vector3.up, hitPoint.normalized);
GameObject newTower = Instantiate(towerPrefab, hitPoint, towerRotation);

Basically you’re already calculating the direction with hitPoint. So all you need to do is point the Vector3.up (the y-axis) in that same direction.

I could have taken it further and translated upward so that it’s less embedded in the earth, but I’d rather have them shorter.

Good luck!

Privacy & Terms