We can use two variables to control the randomness: Let’s add some randomness to the height of the projectile. The ground plane is scaled up so that it’s easier to see in the screenshot above that the random position of the target platform indeed lies on a circle around the projectile. Let’s start with the Update() function: using UnityEngine using System.Collections public class Trajectory : MonoBehaviour When resetting the object, velocity will also be set to 0. For this, we’ll cache the initial position and orientation in Vector3 initialPosition and Quaternion initialRotation at the beginning. The R key will be used to reset the projectile’s position and rotation to their initial values.To calculate the launch speed of the projectile, we’ll use two variables: float LaunchAngle and Transform TargetObject.We’ll look into this in the next section. We can then add some random height to this random target to end up with a 3D location around the projectile. For a random point around the projectile as the target location, we’ll pick a point in a circle surrounding the world origin denoted by the radius float TargetRadius.The projectile will also reset its position to initial position after settings up the new target. Since our projectile will be in one of the two states of setting a new target and launching, we’ll use the bool bTargetReady variable to keep track of the state. We want to cycle through setting a new target location and launching the projectile to the target when Spacebar is pressed.We want to implement the following behavior for the projectile in the Projectile.cs file: Add a Sprite Renderer and set Sprite to InputFieldBackground and Color to red.For a red target mark that would show the current target location, create an empty GameObject and name it to “Mark”.Add a material and attach it to the cube object to color the target platform light gray (or as you will).Set its scale to (0, 0.2, 0) and position to (0, 0.2, 0) to elevate it from the ground a little bit. Create a cube object, name it to “Platform”.Create an Empty Game Object at the root of the scene hierarchy, name it “TargetObject” and place it at position (5, 0, -5).For the target, let’s create a platform with a red mark on it.Create a C# script: Projectile.cs and attach to the capsule object.We’ll need this for setting the velocity of the object. Add a Rigidbody component to the capsule.Add a material and attach it to the plane to color it dark gray (or as you will).To add a ground, add a Plane, reset its transform and adjust its scale to (2, 2, 2).Also, set Clear Flags from Skybox to Solid Color for a less distracting background in play mode.Set camera position to (0, 5, -15) and rotation to (30, 0, 0).
Scene Setup: Ground, Projectile and Target Location objectsĪfter launching Unit圓D and creating a new project: We start by creating a small scene with a few objects: a ground plane, a projectile capsule and a target object which is a platform with a red mark. I’ve used Unity v2017.3.0f3 at the time of preparing this. We’ll start the tutorial with setting up the scene.
the distance the projectile will travel, if we know the launch velocity and the launch angle.the launch angle, if we know how fast we’ll be launching the projectile and where the target is.the velocity, if we know where the target is and what the launch angle will be.To launch the projectile to its target, we will be considering the following variables: Variable Nameīy keeping the other two variables known beforehand, we can calculate any of the variables above. We’ll be using kinematics to achieve our goal, meaning that we’ll only be using velocity in our calculations, disregarding the mass and other acting physical forces on the object that would affect the motion such as drag. You can also see it in action as a WebGL build is also available.
If you want to play around with it yourself, the project files can be found on GitHub. The tutorial will walk you through creating a demo scene for launching a projectile (capsule) to a target location (platform). In this tutorial, we’ll take a closer look at the motion physics of arrows and missiles. Estimated Read Time: ~20 minutes Quick Navigation