Recap: Turret
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 46 of 71.
This recap puts the chapter together: a direction flattened to the ground, Quaternion.LookRotation, a steady turn with RotateTowards, and an offset turned by a rotation.
Challenge
MediumWrite the turret: it tracks the tank and fires shells.
- Each frame, take
tank.position - transform.positionwith itsyset to 0, and turn towardQuaternion.LookRotationof it withQuaternion.RotateTowards, atturnSpeed * Time.deltaTime. - On Space, spawn
shellPrefabattransform.position + transform.rotation * new Vector3(0, 0.5f, 1), withtransform.rotation.
Press Play, drive the tank and fire. Then Check.
Try it yourself
using UnityEngine;
public class Turret : MonoBehaviour
{
public Transform tank;
public GameObject shellPrefab;
public float turnSpeed = 90f;
void Update()
{
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown10Project: Arena Escape
The ArenaHunters2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable WallPractice on your own: Unity playground