Creating a Basic Movement Script in Unity 3D

If you’re new to Unity 3D and looking to create basic movement scripts for your games or interactive applications, you’ve come to the right place. In this article, we will take you through the essential steps of creating a movement script in Unity 3D, using real-life examples and case studies to illustrate the concepts.

Setting up the Character

The first step in creating a basic movement script is to set up your character. You will need to create a new character object in Unity 3D and add a Rigidbody component to it. The Rigidbody component allows you to control how the object moves, so it’s essential for any character that needs to move around.

Once you have added the Rigidbody component to your character, you will need to create an Animator component. The Animator component is used to control animations in Unity 3D, and it’s especially useful when creating characters that need to move around. You can add multiple animations to the Animator component, such as walking, running, and jumping.

To create a new animation for your character, you will need to import an animation clip into Unity 3D. Animation clips are essentially videos that contain animations, which can be imported into Unity 3D and used to control the movements of your characters. You can either create your own animations or use pre-made animations available in the Asset Store.

Once you have imported your animation clip, you will need to drag it onto the Animator component in your character object. You can then add additional animations as needed, and set up transitions between them. This will allow your character to smoothly move from one animation to another.

Creating the Movement Script

Now that we have set up our character, let’s create the movement script. We will use C# for this example, but Unity also supports JavaScript and BooScript.

In the script, you will need to import the necessary namespaces:

csharp
using UnityEngine;
using System.Collections;

Next, you will need to create a few variables to store important information about your character’s movement. For example, you might want to store the current animation clip that your character is playing, as well as the speed at which your character moves.

csharp
public Animator anim;
public float moveSpeed = 5f;

You will also need to create a function that updates your character’s movement every frame. This function should be called every frame by Unity 3D’s Update() method.

csharp
void Update()
{
// code to update character movement goes here
}

Inside this function, you will need to get input from the user to determine which direction your character should move in. You can do this using Unity 3D’s Input class, which provides access to various input devices like keyboard, mouse, and touch screens.

csharp
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

You can then use this input to control your character’s movement. For example, you might set your character’s velocity to move in the desired direction:

csharp
Vector3 targetVelocity = new Vector3(horizontalInput moveSpeed, 0f, verticalInput moveSpeed);
transform.position += targetVelocity * Time.deltaTime;

Finally, you will need to set your character’s current animation clip based on its speed. You can do this using the Animator component that we added earlier:

csharp
if (horizontalInput != 0 || verticalInput != 0)
{
anim.SetFloat("Speed", Mathf.Abs(horizontalInput + verticalInput));
}
else
{
anim.SetFloat("Speed", 0f);
}

Adding Transitions between Animations

In addition to controlling your character’s movement, you may also want to add transitions between animations. For example, you might want your character to smoothly transition from running to walking when it comes to a stop.

To do this, you will need to create an animation clip that contains the various animations you want your character to perform, and then use the Animator component to control the transitions between them.

You can create an animation clip using Unity 3D’s Animation window, which allows you to preview and edit your animations. You can also import pre-made animation clips from external sources like YouTube or other video hosting platforms.

Once you have created your animation clip, you will need to drag it onto the Animator component in your character object. You can then set up transitions between the different animations in the clip using the Animator window.

For example, you might create a transition that goes from running to walking when your character’s speed is low:

csharp
if (anim.GetCurrentAnimatorStateInfo().IsName("Running") && anim.velocitySquaredMagnitude < moveSpeed * moveSpeed)
{
anim.CrossFade("Walking", 1f / 2f);
}

Adding Collision Detection

Finally, you will need to add collision detection to your movement script to allow your character to interact with other objects in the scene. You can do this by adding a Rigidbody component to any object that your character might collide with, and then using Unity 3D’s CollisionManager class to detect collisions between objects.

For example, you might add a Rigidbody component to a wall or floor object in the scene, and then use the following code to detect collisions between your character and that object:

csharp

Adding Collision Detection
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
// handle collision with wall here
}
else if (collision.gameObject.CompareTag("Floor"))
{
// handle collision with floor here
}
}

Conclusion

In this tutorial, we have walked through the basics of creating a movement script for a 3D character in Unity 3D. We have learned how to import animations, control movement using input devices, add transitions between animations, and detect collisions between objects.