#Moving platform moves out of coordinates
1 messages · Page 1 of 1 (latest)
[]itsnotworking
"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?
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;
}
}
[]cb
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.
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?
I don't know, I got this code from the internet 😄
So, ctrl-c , ctrl-v ?
Yes
Also do you have some basic understanding of C#? And the methods you used ( ex, OnCollisionEnter, OnCollisionExit, Update... ) ?
And Unity in general
Yes
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?
No,i already fixed it
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?
I fixed the problem by removing the negative speed, thanks for the help