Nede help with simple object movement script

Hi,
I’m still new to Unity and programming and I’m working on a 3D platformer.
Unfortunately I got a logic issue with a movement script for a moving platform. I wanted the playtform to start mooving when the player is touching it, and to swich its’ direction when it touches a trigger-collider, but somehow my script doesn’t work.
Thank you in advance and sorry for my bad english.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IslandMovement : MonoBehaviour
{
    public float MovementSpeed = 2.0f;
    public bool Move;
    public bool MoveUp;
    ;
    public bool MoveDown;
    // Start is called before the first frame update
    void Start()
    {
        Move = false;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Move == true)
        {
            MoveUp = true;
        }
      

        if (MoveUp == true)
        {
            transform.position += transform.up * MovementSpeed * Time.deltaTime;
        }
        if (MoveDown == true)
        {
            transform.position += transform.up * -MovementSpeed * Time.deltaTime;
        }
    }
  
    void OnCollisionEnter(Collision collision)
    {  
        if (collision.gameObject.Tag == "Player"
        {
             Move = true;
        }
        else
        {
            Move = false;
        }
      
        if (collision.gameObject.tag == "MovementTriggerUp")
        {
            MoveUp = false;
          
        }
    
        if (collision.gameObject.tag == "MovementTriggerDown")
        {
            MoveUp= true;

        }
        if (MoveUp == false)
        {
            if (collision.gameObject.tag == "Player" )
            {
                MoveDown= true;
            }
          
        }
      
    }
}

What do you mean by “doesn’t work”? Do you get an error message? Does it do something but not what you expected it? Does it do nothing?

1 Like

I’m pretty sure OnCollisionEnter is not called when the object intersects with another trigger collider. How I would approach this is you detect the platform entering the trigger collider using a script attached to the GameObject that has the trigger collider. You then have that object tell the platform it is time to turn around. Google for OnTriggerEnter.

Sprinkle Debug.Log statements in your code to see what it is currently doing. I think you’ll find that when it enters the trigger collider that OnCollisionEnter is not called, so setting MoveUp to a new value never occurs.

@Ray_Sovranti
it does nothing

@Joe-Censored
Okay, thank you! I’ll try that and I’ll keep you updatet.:slight_smile:

it will be more helpful if we can see a pic of all the component on the platform object and player object and also a pic of physic layer list

Hi
@Joe-Censored
I found the problem by spreading Debug.Log statements as you recommendet but then I recognized that, for my game, it’ll be easyer to use Invoke and let the platform change its’ direction after a certain time instead of checking for a trigger collider. In the end it turned out working and the script got a lot shorter too so Thanks! :slight_smile:

1 Like