Hi all! Hope this is the right section of the forum. I’m trying to create a moving platform that moves back and forth on Z axes and it correctly does. But, when i rotate the gameobject, for example 90 degrees, it will not anymore come back. Hope you can help me. This is the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlidingMovement : MonoBehaviour
{
public enum LeftOrRight {
Left = -1, Default = 0, Right = 1
}
private float speed = 4.0f;
private Vector3 start_position;
private LeftOrRight platformDirection = LeftOrRight.Default;
// Start is called before the first frame update
void Start()
{
this.platformDirection = LeftOrRight.Right;
this.start_position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
// Update is called once per frame
void Update()
{
transform.position += transform.forward * (int)platformDirection * speed * Time.deltaTime;
if(transform.position.z - start_position.z >= 10.0f) {
SwitchDirection();
} else if (transform.position.z - start_position.z <= -10.0f ) {
SwitchDirection();
}
}
private void SwitchDirection() {
if (this.platformDirection == LeftOrRight.Right)
{
this.platformDirection = LeftOrRight.Left;
} else
{
this.platformDirection = LeftOrRight.Right;
}
}
}