- Home /
Question by
Finchlet · May 26, 2020 at 04:11 PM ·
moving platform
Moving platform not working
I have created a script to move a platform in the z direction, but the platform won't let the player sitting on the platform move around. However, I tried the same code for the x direction and it seems to work perfectly fine. I have been checking my code for any differences between them, but I couldn't find any. Am I missing something? The code for platform moving along the Z-axis:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformMoveZ : MonoBehaviour
{
public float platformSpeed = 0.05f;
public int reachNegative = 1;
private GameObject target = null;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
target = null;
}
// Update is called once per frame
void Update()
{
if (this.transform.position.z <= 30 && reachNegative == 1) {
Vector3 position = this.transform.position;
position.z+=platformSpeed;
this.transform.position = position;
} else if (this.transform.position.z >= 30 && reachNegative == 1) {
reachNegative = 0;
} else if (this.transform.position.z >= 11 && reachNegative == 0) {
Vector3 position = this.transform.position;
position.z-=platformSpeed;
this.transform.position = position;
} else if (this.transform.position.z <= 11 && reachNegative == 0) {
reachNegative = 1;
}
if (target != null) {
target.transform.position = transform.position + offset;
}
}
void OnTriggerStay(Collider other)
{
target = other.gameObject;
offset = target.transform.position - transform.position;
}
void OnTriggerExit(Collider other)
{
target = null;
}
}
The code for platform moving along X-axis:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformMoveX : MonoBehaviour
{
public float platformSpeed = 0.02f;
public int reachNegative = 1;
private GameObject target = null;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
target = null;
}
// Update is called once per frame
void Update()
{
if (this.transform.position.x <= 3 && reachNegative == 1) {
Vector3 position = this.transform.position;
position.x+=platformSpeed;
this.transform.position = position;
} else if (this.transform.position.x >= 3 && reachNegative == 1) {
reachNegative = 0;
} else if (this.transform.position.x >= -3 && reachNegative == 0) {
Vector3 position = this.transform.position;
position.x-=platformSpeed;
this.transform.position = position;
} else if (this.transform.position.x <= -3 && reachNegative == 0) {
reachNegative = 1;
}
if (target != null) {
target.transform.position = transform.position + offset;
}
}
void OnTriggerStay(Collider other)
{
target = other.gameObject;
offset = target.transform.position - transform.position;
}
void OnTriggerExit(Collider other)
{
target = null;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612235200im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving Platform 5 Answers
Can't get platform to move to the other direction 2 Answers
Player Slows Down When Entering a Moving Platform 0 Answers
Moving Platforms 2 Answers