#Hello, i was doing moving platform for my game and i ran into some errors i can´t fix.

1 messages · Page 1 of 1 (latest)

proud monolith
#

So basically, this is my code, i´m beginner so i´m doing it the stupidest way, i know. I got error "CS0501" and it targets the fifth line (the one with void OnCollisionEnter(Collision touchInfo); ). Please, i need some help

using UnityEngine;

public class movingPlatform : MonoBehaviour
{
void OnCollisionEnter(Collision touchInfo);
public Rigidbody rb;
public float speed = 300f;

// Update is called once per frame
void Update()
{
    rb.AddForce(speed, 0, 0);

    if (touchInfo.collider.tag == "odrazeni1") // detekuje kdyz se kostka dotkne prekazky
    {
        rb.AddForce(-speed, 0, 0);
    }

    if (touchInfo.collider.tag == "odrazeni2") // detekuje kdyz se kostka dotkne prekazky
    {
        rb.AddForce(speed, 0, 0);
    }
}

}

#

I started with c# yesterdays so sorry for my bad things

rose grove
proud monolith
#

thank you for resposne, i did it and got this

rose grove
#

using UnityEngine;

public class movingPlatform : MonoBehaviour
{
  public Rigidbody rb;
    public float speed = 300f;
    void OnCollisionEnter(Collision touchInfo)
{
rb.AddForce(speed, 0, 0);

        if (touchInfo.collider.tag == "odrazeni1") // detekuje kdyz se kostka dotkne prekazky
        {
            rb.AddForce(-speed, 0, 0);
        }

        if (touchInfo.collider.tag == "odrazeni2") // detekuje kdyz se kostka dotkne prekazky
        {
            rb.AddForce(speed, 0, 0);
        }
}
  

    
   
}

#

@proud monolith here this should be the code instead

proud monolith
#

I tried this code today, it is not giving me errors anymore but it is not working

#

the platform will move to right but when it touch object it just stops moving and it is not going to move to left

rose grove