Okay, I have set up a script that allows an object to move between two positions and rotations, from point A to point B, and from point B to point A. I am using an event to trigger that, but for some reason, it’s not working. Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeTransforms : MonoBehaviour
{
//holds the object's starting position
[SerializeField]
private Transform sPos;
//holds the object's finishing position
[SerializeField]
private Transform fPos;
//holds the object's move speed
[SerializeField]
private float speed;
//holds the object's rotation speed
[SerializeField]
private float rotationSpeed;
//sets the position and rotation to the start upon starting
private void Awake()
{
this.transform.SetPositionAndRotation(sPos.position, sPos.rotation);
}
//goes from starting position to finishing position
public void GoToFinish()
{
var step = speed * Time.deltaTime;
var rotStep = rotationSpeed * Time.deltaTime;
this.transform.position = Vector3.MoveTowards(this.transform.position, fPos.position, step);
this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, fPos.rotation, rotStep);
//checks if the position of the object and its destination are approximately equal
if(Vector3.Distance(this.transform.position, fPos.position) < 0.001f)
{
this.transform.position = fPos.position;
}
}
//goes from finishing position to starting position
public void GoToStart()
{
var step = speed * Time.deltaTime;
var rotStep = rotationSpeed * Time.deltaTime;
this.transform.position = Vector3.MoveTowards(this.transform.position, sPos.position, step);
this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, sPos.rotation, rotStep);
//checks if the position of the object and its destination are approximately equal
if (Vector3.Distance(this.transform.position, sPos.position) < 0.001f)
{
this.transform.position = sPos.position;
}
}
}
What am I doing wrong here? What would I need to change?