Question by
Regina21997 · Apr 03, 2018 at 04:56 AM ·
issuesmoving platform
How to get my moving platform to move from position1 to position2 constantly so that my player can be taken from one point to another?
Hey :). So I am new to unity and scripting. I am using c sharp. I am trying to get my moving platform to move correctly from one position to another and repeat this function constantly so that my player can get onto it and go from one point to another. This is my script at the moment. The issue with this is that my platform moves from position 1 to position 2 but then flies off somewhere and disappears :(. I can't work out why and how to make that right. Please help me out on this :S.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlatformMovement : MonoBehaviour{
public Transform movingplatform;
public Transform position1;
public Transform position2;
public float movingplatformSpeed;
Vector3 direction;
Transform destination;
void Start(){
SetDestination (position1);
}
void FixedUpdate(){
movingplatform.GetComponent<Rigidbody>().MovePosition(movingplatform.position + direction * movingplatformSpeed * Time.fixedDeltaTime);
if(Vector3.Distance (movingplatform.position, destination.position) < movingplatformSpeed * Time.fixedDeltaTime)
SetDestination(destination = position1 ? position2 : position1);
}
void OnDrawGizmos(){
Gizmos.color = Color.green;
Gizmos.DrawWireCube(position1.position, movingplatform.localScale);
Gizmos.color = Color.red;
Gizmos.DrawWireCube(position2.position, movingplatform.localScale);
}
void SetDestination(Transform dest){
destination = dest;
direction = (destination.position - movingplatform.position).normalized;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612155920im_/https://answers.unity.com/themes/thub/images/avi.jpg)