#Moving platform moves out of coordinates

1 messages · Page 1 of 1 (latest)

maiden coral
#

The moving platform does not move along the coordinates, but everything seems to be fine in the script.

sinful ice
#

[]itsnotworking

honest orioleBOT
#

"It's not working" is not helpful

In order for a question to be answered, it must specify what exactly is wrong. Stating simply that "it doesn't work" is not sufficient.
Source: https://idownvotedbecau.se/itsnotworking

Please elaborate on your question by including all relevant details. What do you think is the problem? Have you tried to fix it? If you have, why didn't that work?

sinful ice
#

Also may I see the code?

maiden coral
# sinful ice Also may I see the code?

using UnityEngine;

public class RightLeftMovePlatform:MonoBehaviour
{

[SerializeField] private float speed;

[SerializeField] private float startingPosition;

[SerializeField] private float endPosition;

private bool upMove = true;

private Vector3 pos;

void Update()
{

    transform.position += transform.right * speed * Time.deltaTime;

    if (pos.x > endPosition && upMove)
    {

        upMove = false;

        speed *= -1f;
    }

    if (pos.x < startingPosition && !upMove)
    {

        upMove = true;

        speed *= -1f;
    }

    pos = transform.position;
}

void OnCollisionEnter(Collision col)
{

    col.gameObject.transform.parent = transform;

}

void OnCollisionExit(Collision col)
{

    col.gameObject.transform.parent = null;

}

}

sinful ice
#

[]cb

honest orioleBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

sinful ice
#

It would look something like this :

using UnityEngine;

public class RightLeftMovePlatform:MonoBehaviour
{

    [SerializeField] private float speed;

    [SerializeField] private float startingPosition;

    [SerializeField] private float endPosition;

    private bool upMove = true;

    private Vector3 pos;

    void Update()
    {

        transform.position += transform.right * speed * Time.deltaTime;

        if (pos.x > endPosition && upMove)
        {

            upMove = false;

            speed = -1f;
        }

        if (pos.x < startingPosition && !upMove)
        {

            upMove = true;

            speed= -1f;
        }

        pos = transform.position;
    }

    void OnCollisionEnter(Collision col)
    {

        col.gameObject.transform.parent = transform;

    }

    void OnCollisionExit(Collision col)
    {

        col.gameObject.transform.parent = null;

    }

}
#

What are those "sqaure" child objects of the leftrightplatforms?

maiden coral
sinful ice
#

So, ctrl-c , ctrl-v ?

maiden coral
sinful ice
#

Also do you have some basic understanding of C#? And the methods you used ( ex, OnCollisionEnter, OnCollisionExit, Update... ) ?

#

And Unity in general

sinful ice
#

If I just look at the video I can see that the start and end positions are : 8.48 - 0.51 but the x value is negative, I suggest you looking at the upMove bool and see if it is setting things correctly

#

Wait.... Now I see

#

Here :

if (pos.x > endPosition && upMove)
        {

            upMove = false;

            speed = -1f;
        }

        if (pos.x < startingPosition && !upMove)
        {

            upMove = true;

            speed= -1f;
        }

So in both cases the speed should be -1f?

maiden coral
sinful ice
#

Very good then! Very good if you figured out the problem yourself. But may I ask what was causing the problem and what you changed?

maiden coral