Search Unity

Nede help with simple object movement script

Discussion in 'Scripting' started by therealnix, Feb 23, 2021.

  1. therealnix

    therealnix

    Joined:
    Feb 23, 2021
    Posts:
    3
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IslandMovement : MonoBehaviour
    6. {
    7.     public float MovementSpeed = 2.0f;
    8.     public bool Move;
    9.     public bool MoveUp;
    10.     ;
    11.     public bool MoveDown;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         Move = false;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void FixedUpdate()
    20.     {
    21.         if (Move == true)
    22.         {
    23.             MoveUp = true;
    24.         }
    25.      
    26.  
    27.         if (MoveUp == true)
    28.         {
    29.             transform.position += transform.up * MovementSpeed * Time.deltaTime;
    30.         }
    31.         if (MoveDown == true)
    32.         {
    33.             transform.position += transform.up * -MovementSpeed * Time.deltaTime;
    34.         }
    35.     }
    36.  
    37.     void OnCollisionEnter(Collision collision)
    38.     {  
    39.         if (collision.gameObject.Tag == "Player"
    40.         {
    41.              Move = true;
    42.         }
    43.         else
    44.         {
    45.             Move = false;
    46.         }
    47.      
    48.         if (collision.gameObject.tag == "MovementTriggerUp")
    49.         {
    50.             MoveUp = false;
    51.          
    52.         }
    53.    
    54.         if (collision.gameObject.tag == "MovementTriggerDown")
    55.         {
    56.             MoveUp= true;
    57.  
    58.         }
    59.         if (MoveUp == false)
    60.         {
    61.             if (collision.gameObject.tag == "Player" )
    62.             {
    63.                 MoveDown= true;
    64.             }
    65.          
    66.         }
    67.      
    68.     }
    69. }
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    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?
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  4. therealnix

    therealnix

    Joined:
    Feb 23, 2021
    Posts:
    3
  5. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    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
     
  6. therealnix

    therealnix

    Joined:
    Feb 23, 2021
    Posts:
    3
    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! :)
     
    Joe-Censored likes this.