Today, we’ll walk through the steps to create a slime character that moves horizontally across platforms in Unity. The slime will detect the edges of the platform and change direction to avoid falling off. We’ll also ensure the slime’s movement is smooth and glitch-free.
You can get started by downloading the free slime asset from here. Thanks to the artist.
Step 1: Setting Up the Scene
- Create the Slime GameObject:
- Add a new GameObject to your scene and name it “Slime”.
- Attach a Rigidbody2D component to the slime.
- Attach a
BoxCollider2Dcomponent to the slime. - Attach an Animator component to the slime if you have animations.
- Create the Ground Check:
- Create an empty GameObject as a child of the slime and name it “GroundCheck”.
- Position the
GroundCheckslightly below the slime to detect the platform edges.
- Set Up the Platform Layer:
- Ensure your platforms have a
BoxCollider2Dand are assigned to a specific layer (e.g., “Platform”).
- Ensure your platforms have a
Step 2: Creating the SlimeMovement Script
Create a new C# script named SlimeMovement and attach it to the slime GameObject. Here’s the complete script:
using UnityEngine;
public class SlimeMovement : MonoBehaviour
{
public float speed = 2f;
public Transform groundCheck;
public LayerMask platformLayer;
private Rigidbody2D rb;
private Animator animator;
private bool movingRight = true;
private float groundCheckDistance = 0.1f;
private float edgeCheckDistance = 0.1f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
Move();
CheckPlatform();
}
void Move()
{
float moveDirection = movingRight ? 1 : -1;
rb.velocity = new Vector2(moveDirection * speed, rb.velocity.y);
// animator.SetBool("isMoving", true);
}
void CheckPlatform()
{
// Check if there is ground ahead
Vector2 groundCheckPosition = groundCheck.position + new Vector3(movingRight ? edgeCheckDistance : -edgeCheckDistance, 0, 0);
RaycastHit2D groundInfo = Physics2D.Raycast(groundCheckPosition, Vector2.down, groundCheckDistance, platformLayer);
if (groundInfo.collider == null)
{
// Change direction if no ground ahead
movingRight = !movingRight;
transform.localScale = new Vector3(movingRight ? 1 : -1, 1, 1);
}
}
}
Step 3: Configuring the Script
- Assign the Ground Check:
- Drag the
GroundCheckGameObject to the groundCheck field in the SlimeMovement script.
- Drag the
- Set the Platform Layer:
- Set the platformLayer field in the SlimeMovement script to the layer assigned to your platforms.
Step 4: Testing the Movement
- Play the Scene:
- Press the play button in Unity to test the slime’s movement.
- The slime should move horizontally and change direction when it reaches the edge of the platform.
- Adjust Parameters:
- Adjust the speed, groundCheckDistance, and
edgeCheckDistanceparameters in the SlimeMovement script to fine-tune the slime’s movement.
- Adjust the speed, groundCheckDistance, and
Conclusion
By following these steps, you have created a slime character that moves horizontally across platforms, detects edges, and changes direction to avoid falling off. This simple yet effective movement script can be a great starting point for more complex AI behaviors in your Unity projects.
Discover more from TheFlipbit
Subscribe to get the latest posts to your email.
