Search Unity

Interactive object example

Discussion in 'Scripting' started by RobAnthem, May 15, 2020.

  1. RobAnthem

    RobAnthem

    Joined:
    Dec 3, 2016
    Posts:
    90
    This is an example of an interactive object that moves the player to it. This script assumes you have a controller to call to it. I'll write another script showing how to do that. For anyone who doesn't know.

    Code (CSharp):
    1. using UnityEngine;
    2. public class Interactable : MonoBehaviour
    3. {
    4.     //If your player doesn't exist at start of scene you'll need to FIndPlayer on awake
    5.     public GameObject playerObject;
    6.     //The target position is the position you want to the player to be, and the rotation you want them to have.
    7.     // So rotate the targets to the rotation the player will end at.
    8.     public Transform targetPosition, exitPosition;
    9.     /// <summary>
    10.     /// The minimum angle the player can be facing to the end facing direction.
    11.     /// </summary>
    12.     public float targetAngle = 45;
    13.     /// <summary>
    14.     /// Just movement fudge.
    15.     /// </summary>
    16.     public float MoveMargin = .05f;
    17.     /// <summary>
    18.     /// Speed at which the player will move, in meters per second.
    19.     /// </summary>
    20.     public float MovementSpeed = 5.0f;
    21.     /// <summary>
    22.     /// Speed at which the player will rotate in degrees per second.
    23.     /// </summary>
    24.     public float RotationSpeed = 30.0f;
    25.     /// <summary>
    26.     /// if the player is currently moving
    27.     /// </summary>
    28.     private bool moving;
    29.     /// <summary>
    30.     /// the current target of the player
    31.     /// </summary>
    32.     private Transform currentTarget;
    33.     /// <summary>
    34.     /// Call this from your raycast script
    35.     /// </summary>
    36.     public void MovePlayerToTarget()
    37.     {
    38.         if (Vector3.Angle(playerObject.transform.forward, targetPosition.transform.forward) > targetAngle)
    39.             return;
    40.         if (moving)
    41.             return;
    42.         moving = true;
    43.         currentTarget = targetPosition;
    44.     }
    45.     /// <summary>
    46.     /// Call this from your control script for leaving an action, I assume it's all one script
    47.     /// </summary>
    48.     public void MovePlayerToExit()
    49.     {
    50.         if (moving)
    51.             return;
    52.         moving = true;
    53.         currentTarget = exitPosition;
    54.     }
    55.     void Update()
    56.     {
    57.         if (!moving)
    58.             return;
    59.         if (Vector3.Distance(playerObject.transform.position, currentTarget.position) > MoveMargin ||
    60.             Vector3.Angle(playerObject.transform.forward, currentTarget.transform.forward) > MoveMargin)
    61.         {
    62.             playerObject.transform.position = Vector3.MoveTowards(playerObject.transform.position, currentTarget.position, MovementSpeed * Time.deltaTime);
    63.             playerObject.transform.rotation = Quaternion.RotateTowards(playerObject.transform.rotation, currentTarget.rotation, RotationSpeed * Time.deltaTime);
    64.         }
    65.         else
    66.         {
    67.             PlayerReachedTarget();
    68.         }
    69.     }
    70.     public void PlayerReachedTarget()
    71.     {
    72.         moving = false;
    73.         //anything else you want to happen here, like calling back to your character controller to enable input
    74.     }
    75. }
     
    Last edited: May 15, 2020