Okay, I have made a script that has public functions tied to an event to move and rotate an 3D object from one position and rotation to another. However, when I try to activate it via an event, nothing happens. 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 could I be doing wrong? At what line is everything falling apart?