TopDown conveyor belt, almost working!
Hi all, I think am close to finishing this script for a conveyor belt but it is not quite working. The idea is of course for the player to walk on to a space and be pushed in a certain direction. Right now when the player walks into the conveyor belt object's trigger the 'touched conveyorLeft' debug does go off, and the playerConveyorLeft variable does turn true (confirmed in the inspector during run-time), and when the player exits the trigger the variable successfully turns back to false. The problem is on the player script when it checks to see if the playerConveyorLeft variable is true or false. It does not want to be able to do that properly and the debug in that if statement will not go off and hence it will not proceed to the final part of the script where movement is actually applied to the player creating the desired 'conveyor belt' effect. No other errors or notifications appear in the console. I be this would be a useful script to others as well if it was tweaked just a bit to get it working proper. Thanks in advance!
The conveyor belt:
public bool conveyorPlayerLeft = false, conveyorEnemyLeft = false, conveyorBlockLeft = false;
void OnTriggerEnter2D(Collider2D target)
{
if (target.gameObject.tag == "Player") {
Debug.Log ("touched conveyorleft");
conveyorPlayerLeft = true;
}
}
void OnTriggerExit2D(Collider2D target)
{
if (target.gameObject.tag == "Player") {
Debug.Log ("exited conveyor");
conveyorPlayerLeft = false;
}
}
And the player:
public conveyorBeltLeft conveyorScript;
public GameObject conveyorReference;
private void Start()
{
playerRigid = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (conveyorReference.GetComponent<conveyorBeltLeft>().conveyorPlayerLeft) {
Debug.Log ("got to playerConveyor If");
playerRigid.velocity = new Vector2(-10, 0);
}