Make an object stationary until player is colliding with it, then move along path.
I've been trying to make a script for an elevator. So far I've got the movement part done but I can't figure out how to stop the script from starting before the player is on the elevator object.
This is what I've got so far :
public class LiftScript : MonoBehaviour
{ private Vector3 posA;
private Vector3 posB;
[SerializeField]
private float speed;
[SerializeField]
private Transform startPosition;
[SerializeField]
private Transform endPosition;
void Start()
{
posA = startPosition.localPosition;
posB = endPosition.localPosition;
}
void Update()
{
Move();
}
private void Move()
{
startPosition.localPosition = Vector3.MoveTowards(startPosition.localPosition, posB, speed * Time.deltaTime);
}
}
Answer by KevRev · Apr 23, 2019 at 11:04 PM
I'd probably have a collider, smaller than the lift, in the centre, and when the player collides with it, start the movement. This code should help point you in the right direction:
void OnCollisionEnter()
{
Moving=true;
}
void OnCollisionExit()
{
Moving=false;
}
void Update ()
{
if (Moving) Move();
}
Took me a $$anonymous$$ute to fill in the blanks but I ended up figuring out a way of doing it. Not sure if it's the way you were thinking of doing it but what I came up with is below. I also put in a set parent so that the player wouldn't slide off.
public class LiftScript : $$anonymous$$onoBehaviour
// This Script creates a platform that moves from its original position to a customisable secondary location and back again. The player will follow this platform if touching it { private Vector3 posA;
private Vector3 posB;
private Vector3 nextPos;
[SerializeField]
private float speed;
[SerializeField]
private Transform startPosition;
[SerializeField]
private Transform endPosition;
private bool moving;
// Use this for initialization
void Start()
{
posA = startPosition.localPosition;
posB = endPosition.localPosition;
nextPos = posB;
}
private void ChangeDestination()
{
nextPos = nextPos != posA ? posA : posB;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
moving = true;
collision.collider.transform.SetParent(transform);
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
collision.collider.transform.SetParent(null);
}
}
private void FixedUpdate()
{
if (moving)
{
startPosition.localPosition = Vector3.$$anonymous$$oveTowards(startPosition.localPosition, nextPos, speed * Time.deltaTime);
}
}
}
Perfect! Well done :)
$$anonymous$$aybe also add a line something like the following:
private void FixedUpdate()
{
if (moving && startPosition!=nextPos)
{
startPosition.localPosition = Vector3.$$anonymous$$oveTowards(startPosition.localPosition, nextPos, speed * Time.deltaTime);
}
else {
moving=false;
ChangeDestination();
}
}
What this should do, is once it reaches nextPos, it stops moving and swaps the destination. Then, on the next collision, it'll move again to its initial position.