- Home /
How to use rigidbody.MovePosition within OnTrigger?
I'm trying to make a platform that follows the player as he moves on the x coordinate. So far its working well, except for one thing--collisions. In order to put in some basic puzzle-solving, the platform needs to collide with other platforms so it can stop following the player. Right now I have this:
using UnityEngine;
using System.Collections;
public class FollowPlatform : MonoBehaviour
{
public float speed = 1f;
public Rigidbody myRigidbody;
void Start ()
{
myRigidbody=GetComponentInChildren<Rigidbody>();
}
void OnTriggerStay(Collider otherObject)
{
if (otherObject.transform.position.x>transform.position.x+2)
transform.Translate(Vector3.right * speed);
if (otherObject.transform.position.x<transform.position.x-2)
transform.Translate(Vector3.right * speed * -1);
}
}
this is attached to the collider just above the platform itself, so that the player lands in the collider and it starts moving, thus dragging the childed platform along with it. The problem is, obviously, that using transform.Translate in this context would completely overlook any collisions. I'm trying to use rigidbody.MovePosition instead, but its not working at all as expected. Any help with this would be greatly appreciated.
Your answer
Follow this Question
Related Questions
Character Slowly sliding off platform 1 Answer
Trigger Rigidbody with CharacterController 0 Answers
How can I call OnTriggerEnter2D from the object with the trigger on it? 2 Answers
Character is pushed off platform only when moving 0 Answers
OnTriggerEnter not being called, have trigger, RB, and colliders set? 1 Answer