Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCharacterToPosition : MonoBehaviour
{
    // Reference to the CharacterController component
    public CharacterController characterController;

    // Trigger to activate the function
    public bool moveAndFire = false;

    // Movement speed
    public float moveSpeed = 5.0f;

    // Distance and direction variables
    private Vector3 distance;
    private Vector3 direction;

    // Initialize distance and direction values
    void Start()
    {
        distance = new Vector3(10.0f, 0.0f, 10.0f);
        direction = Vector3.forward;
    }

    // Update is called once per frame
    void Update()
    {
        // Check if the trigger is activated
        if (moveAndFire)
        {
            MoveCharacterToPositionAndFire(distance, direction);
        }
    }

    // MoveCharacterToPositionAndFire function
    public void MoveCharacterToPositionAndFire(Vector3 distance, Vector3 direction)
    {
        // Calculate the target position
        Vector3 targetPosition = transform.position + direction.normalized * distance.magnitude;

        // Move the character to the target position
        characterController.Move((targetPosition - transform.position).normalized * moveSpeed * Time.deltaTime);
    }
}


MoveCharacterToPosition Documentation

Overview

The MoveCharacterToPosition script is designed to move a character using Unity’s CharacterController component. It provides a simple way to move a character to a specified position, with the option to easily trigger the movement from other scripts or events.

Requirements

  • Unity 2020.3.0f1 or later (recommended)
  • A GameObject with a CharacterController component attached

Setup

  1. Create a new GameObject in your scene or select an existing one.
  2. Attach a CharacterController component to the GameObject if it doesn’t have one already.
  3. Create a new C# script in your project, name it MoveCharacterToPosition, and open it in your preferred code editor.
  4. Copy and paste the provided MoveCharacterToPosition script into your newly created script file, and save it.
  5. Attach the MoveCharacterToPosition script to your GameObject in the Unity Editor.
  6. In the Inspector window, set the CharacterController field to reference the CharacterController component on your GameObject.

Parameters

  • characterController: A reference to the CharacterController component on your GameObject.
  • moveAndFire: A boolean trigger that activates the MoveCharacterToPositionAndFire function when set to true.
  • moveSpeed: The speed at which the character moves (default: 5.0f).

Functions

MoveCharacterToPositionAndFire(Vector3 distance, Vector3 direction)

This function moves the character to a specified position based on the given distance and direction.

  • distance: A Vector3 representing the distance the character should move.
  • direction: A Vector3 representing the direction in which the character should move.

Usage

To use the MoveCharacterToPosition script in other scripts, you can follow these steps:

  1. In your other script, add a reference to the MoveCharacterToPosition script:
    public MoveCharacterToPosition moveCharacterToPosition;
    
  2. Assign the reference to the MoveCharacterToPosition component in the Unity Editor by dragging and dropping the GameObject with the MoveCharacterToPosition script attached onto the exposed field in the Inspector.
  3. In your other script, you can now call the MoveCharacterToPositionAndFire function:
    // Define the distance and direction
    Vector3 distance = new Vector3(10.0f, 0.0f, 10.0f);
    Vector3 direction = Vector3.forward;

    // Call the MoveCharacterToPositionAndFire function
    moveCharacterToPosition.MoveCharacterToPositionAndFire(distance, direction);

  4. If you want to use the moveAndFire trigger, simply set it to true in your other script:
    moveCharacterToPosition.moveAndFire = true;
    

    This will cause the character to move when the Update() function of the MoveCharacterToPosition script is called. Remember to set it back to false after the character has moved, to prevent continuous movement.

That’s it! Now you can use the MoveCharacterToPosition script to control character movement in your Unity project.