For everyone who does not have their platform moving: Do exactly as he does, make empty gameobject(Moving_Platform), place platform inside of it and make pos1 and pos2 objects be children of an empty gameobject(Moving_Platform).
if people have problems. think about it. check your player script. most player movement scripts use rigidbody velocity. if you press a button you change velocity on x or y. if you dont press a button you make velocity of x and y zero. you WRITE zero every frame. so no matter if you parent the player to the moving platform. you dont get keystrokes so you write velocity zero to the player and he doesnt move with the platform. you need to change your player script. instead of parenting you could call your playerscript from the moveing platform and set a BOOL like _isOnMovingPlatform true or false and then copy the velocity of the moving platform. ooooooooor. yes, you could parent it, and if its true _isOnMoving Platform then dont write velocity zero onto your player rigidbody etc.... just a few thoughts.
Is there any problem here? Here is my code: (I think you need some fix) using System.Collections; using System.Collections.Generic; using UnityEngine; public class MovingPlatform : MonoBehaviour { public Transform pos1, pos2; public float speed; public Transform startPos; Vector3 nextPos; // Start is called before the first frame update void Start() { nextPos = startPos.position; } // Update is called once per frame void Update() { if (transform.position == pos1.position) { nextPos = pos2.position; } if (transform.position == pos2.position) { nextPos = pos1.position; } transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime); } private void OnDrawGizmos() { Gizmos.DrawLine(pos1.position, pos2.position); } }
I fixed this script using System.Collections; using System.Collections.Generic; using UnityEngine; public class MovingPlatform : MonoBehaviour { public Transform pos1, pos2; public float platformSpeed; Vector3 nextPos;
So I have a problem with my moving platform. I followed the exact procedures shown in the video, and my platform does move, but for some reason it is invisible in my Game view. I tried changing the layers of the sprites, but it didn't work. Do you know how this can be fixed?
I am completely new to game development, I followed brackeys 2d game series. my problem was the player keeps falling from the platform even after trigger, so added a new collider to the platform without trigger and set the rigid body gravity scale to zero. solved. I hope somebody find this helpful
I have a problem.. I'm pretty shore I followed up step by step but still my platform sins I press "play game" start's moving to the position 1 and stops there, and that's all..
Hey, your channel has been really useful as you explain everything really well! Thanks for making these as they really help. Also, if you ever have the time I'd love to know how to make projectiles that you can shoot. In the game I'm making you have to throw spear like objects that can get stuck in walls and I'm really not sure how to do it. It's a bit complicated but it would be really nice to even just get a nudge in the right direction!
@@xlaugts151 friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them .. how do I double jump from the video code .. thanks
Hey Aliagha, thank you for sharing with me. I will come back probable in a month with the channel and sort all these problems too. If you have any suggestion for a video let me know. Cheers
Hi how can i use it for a new scene? It works for my main game scene but when I try to use it for a new scene it slowly moves, stops at pos1 then disappears.
how do you have multiple platforms in a scene? everything physically works but one of the sprites follows the others movement despite the collider not being there
transform coordinates of pos1 and platform are different when i try to do this. i set the x coordinate of pos1 to 5 but when i set the platform to 5 as well it goes to another position in the scene. Is this something about world position and the local position? I couldn't figure it out
If you set the moving platform to be moving at player eye level and also across where idle player stands, this pushes the player. How about if you want player to remain still so that platform moves freely behind player layer without pushing the player?
friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them .. how do I double jump from the video code .. thanks
I am having issues with this. (error CSO117: 'Time' Does not contain a definition for 'deltatime') But that's not the only error. Its just filled with them.
hey, i can stay on platform, but when i am starting to move form left to right, player moves like... 10fps, can you help me? this is my player script using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerCtrl : MonoBehaviour { public float speed; public float jumpForce; private float moveInput; private Rigidbody2D rb; private bool facingRight = true; public bool isGrounded; public Transform groundCheck; public float checkRadius; public LayerMask whatIsGround; private int extraJumps; public int extraJumpsValue;
Nice video. My platform moves but I have one problem and I can't find out why it is like that. It doesn't detect that it is grounded if it is on the platform even though my script should work as it works at the horizontal moving platforms. This problem effects as well that the collider of my player doesn't work like it sould as it rotates instead of moving forwards
Hey, thanks heaps for the tutorial but for some reason my platform just dissapears after a few seconds rather than moving. The box collider is still there and I can see it in the same place in the scene view but not in the game.
Does you scene has a background ??? It could be the layers, so sometimes the sprite of the platform is on a lower layer lvl than the background so the platform sprite wont show
@@xlaugts151 When i started the game the platform was there then when it should have moved it disappeared, its fine now though i used a different method
@@SyahirahLee i would love to help but i can not remember what i did. Im pretty sure I found a different tutorial that did it slightly differently. Sorry I couldnt help
Plz explain your code my code is not the same as yours so when u put variables that u don’t explain I don’t know how to implement it into my code because you don’t explain
Hi Cynical, Check the positions, pos1 and pos2. If change direction only when hits something, it might be something with the pos1 and 2. Share the script and I will be able to help you. Thanks
@@xlaugts151 friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them .. how do I double jump from the video code .. thanks
great tutorials! if it's ok... as a part making 2d game, can you show how to make intro scene that the user ask to type his name so in the end of the level (in another scene) will be a massage "congratulations john you collect 5 coins".
Just use playerprefs. It'll save the users name once the user finishes typing on the input field (can be customized) or you can tell them to press a button or hit enter when their done.
Mine just falls out the sky :s edit: it does work just not in the positions i told it to goes to random ones of its own accord. Edit again i fixed this by adding the script and collider to the parent instead of the child. I'm pretty sure you actually said to do this also xD also remember to turn gravity off
This is not a good tutorial. Your code is bad. You are checking whether or not the platform's position is exactly equal to pos1 or pos2. In 99% of cases, the platform will never be at exactly that position. Since you're using MoveTowards, the platform will overshoot its destination, and then overshoot it by the same amount in the other direction, endlessly.
For everyone who does not have their platform moving: Do exactly as he does, make empty gameobject(Moving_Platform), place platform inside of it and make pos1 and pos2 objects be children of an empty gameobject(Moving_Platform).
if people have problems. think about it. check your player script. most player
movement scripts use rigidbody velocity. if you press a button you change velocity on x or y. if you dont press a button you make velocity of x and y zero. you WRITE zero every frame. so no matter if you parent the player to the moving platform. you dont get keystrokes so you write velocity zero to the player and he doesnt move with the platform. you need to change your player script. instead of parenting you could call your playerscript from the moveing platform and set a BOOL like _isOnMovingPlatform true or false and then copy the velocity of the moving platform. ooooooooor. yes, you could parent it, and if its true _isOnMoving Platform then dont write velocity zero onto your player rigidbody etc.... just a few thoughts.
Thank you.... I had to do a little adjustment to make it work in 3d but it works like a charm....
why doesn't the player stick onto the platform for me
Hey!
Have you solved the issue?
hey, i have a problem that my character doesnt move with the platform when standing on it, he keeps staying in the same play i land on
My platform goes to pos1 and stops. What did I do wrong?
try creating the pos gameobjects outside the original gameobject you are trying to move
@@maph420 This is actually how I ended up fixing the problem back about a year ago, so credits to u for figuring it out!
@@darkergood5248 HAHAH omg didn't see the date when I answered :p, anyway keeps being useful for new people with the same issue
@@maph420 Yes, thanks a lot :D
platform doesn't render I have collision but now actuall model shows up dispite one being in scene view no background layer is obstructing it
Is there any problem here?
Here is my code: (I think you need some fix)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlatform : MonoBehaviour
{
public Transform pos1, pos2;
public float speed;
public Transform startPos;
Vector3 nextPos;
// Start is called before the first frame update
void Start()
{
nextPos = startPos.position;
}
// Update is called once per frame
void Update()
{
if (transform.position == pos1.position)
{
nextPos = pos2.position;
}
if (transform.position == pos2.position)
{
nextPos = pos1.position;
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
private void OnDrawGizmos()
{
Gizmos.DrawLine(pos1.position, pos2.position);
}
}
I fixed this script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlatform : MonoBehaviour
{
public Transform pos1, pos2;
public float platformSpeed;
Vector3 nextPos;
void Start()
{
nextPos = pos1.position;
}
void FixedUpdate()
{
if (transform.position == pos1.position)
nextPos = pos2.position;
if (transform.position == pos2.position)
nextPos = pos1.position;
transform.position = Vector2.MoveTowards(transform.position, nextPos, platformSpeed * Time.deltaTime);
}
private void OnDrawGizmos()
{
Gizmos.DrawLine(pos1.position, pos2.position);
}
}
@@wiedzmiz2982 what is a different thing?
@@yahyaaliahmed3466 in void start i use nextPos = pos1.position; no nextPos = startPos.position;
@@wiedzmiz2982 yea thank you. because startPos start empty
@@wiedzmiz2982 ty i was having problems as well!
So I have a problem with my moving platform. I followed the exact procedures shown in the video, and my platform does move, but for some reason it is invisible in my Game view. I tried changing the layers of the sprites, but it didn't work. Do you know how this can be fixed?
check to see if your prefab has the transforms reset to 0's
I have the same problem
Check your z axis and make sure it's in front of the camera. If your object is behind your camera the layer wont matter.
@@JTAGames This fixed it for me! I I was too caught up with the layers :,)
this is happening to me as well
nice tutorial liked
when i add rigidbody2d to the platform it falls, how can i stop that?
ok i fixed it. i just checked the constraint checkboxes of rigidbody2d
I am completely new to game development, I followed brackeys 2d game series. my problem was the player keeps falling from the platform even after trigger, so added a new collider to the platform without trigger and set the rigid body gravity scale to zero. solved. I hope somebody find this helpful
Thank you very much, you saved my day bro
Finally! Been looking for a solution for hours! Thanks for this mate!
I have a problem.. I'm pretty shore I followed up step by step but still my platform sins I press "play game" start's moving to the position 1 and stops there, and that's all..
Hey, your channel has been really useful as you explain everything really well! Thanks for making these as they really help. Also, if you ever have the time I'd love to know how to make projectiles that you can shoot. In the game I'm making you have to throw spear like objects that can get stuck in walls and I'm really not sure how to do it. It's a bit complicated but it would be really nice to even just get a nudge in the right direction!
it is working but player is changing its size automatically after moving off the platform.
Can u make a 2d ladder tutorial?
hello the script did not work here. I added the script to the platform and tagged it but my character keeps falling off the platform.
Hi Gamer,
Is your character falling of the platform ? is the boxcollider attached properly to the platform ?
@@xlaugts151 friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them ..
how do I double jump from the video code .. thanks
How can I use this if I am not using update function?
me: peacefully creates a platform
me 5 min later reading the comments: :OOOO
inniiiiiiiiiiiiiit
Thanks for this great explanation. Keep it up.
there is no other scripts on git hub project files :/
Hey Aliagha, thank you for sharing with me. I will come back probable in a month with the channel and sort all these problems too. If you have any suggestion for a video let me know.
Cheers
Hi how can i use it for a new scene? It works for my main game scene but when I try to use it for a new scene it slowly moves, stops at pos1 then disappears.
how do you have multiple platforms in a scene? everything physically works but one of the sprites follows the others movement despite the collider not being there
transform coordinates of pos1 and platform are different when i try to do this. i set the x coordinate of pos1 to 5 but when i set the platform to 5 as well it goes to another position in the scene. Is this something about world position and the local position? I couldn't figure it out
It didin't work for me :S
Hi Frederico
Is there any error ?
If you set the moving platform to be moving at player eye level and also across where idle player stands, this pushes the player. How about if you want player to remain still so that platform moves freely behind player layer without pushing the player?
Great tutorial man! Keep it up :)
friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them ..
how do I double jump from the video code .. thanks
thx so much
I am having issues with this. (error CSO117: 'Time' Does not contain a definition for 'deltatime')
But that's not the only error.
Its just filled with them.
you have to capitalize it like deltaTime
hey, i can stay on platform, but when i am starting to move form left to right, player moves like... 10fps, can you help me?
this is my player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerCtrl : MonoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
public bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
private void Start()
{
rb = GetComponent();
extraJumps = extraJumpsValue;
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(facingRight == false && moveInput > 0)
{
Flip();
}else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
private void Update()
{
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
} else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = 10;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = 5;
}
}
}
Nice video. My platform moves but I have one problem and I can't find out why it is like that. It doesn't detect that it is grounded if it is on the platform even though my script should work as it works at the horizontal moving platforms. This problem effects as well that the collider of my player doesn't work like it sould as it rotates instead of moving forwards
Abi türk değilsin ama ben yine de yazayım, abi taşşaklarına kurban çok işime yaradı hiç bi yerde bulamamıştım eyw.
Hey, thanks heaps for the tutorial but for some reason my platform just dissapears after a few seconds rather than moving. The box collider is still there and I can see it in the same place in the scene view but not in the game.
Does you scene has a background ??? It could be the layers, so sometimes the sprite of the platform is on a lower layer lvl than the background so the platform sprite wont show
@@xlaugts151 When i started the game the platform was there then when it should have moved it disappeared, its fine now though i used a different method
@@mentionethan Good Job!
@@mentionethan hey, mine happened the same thing as yours, do you think you could share how you solved it? thanks!
@@SyahirahLee i would love to help but i can not remember what i did. Im pretty sure I found a different tutorial that did it slightly differently. Sorry I couldnt help
Thanks, helped a lot!
Thanks mate. Nice video.
Plz explain your code my code is not the same as yours so when u put variables that u don’t explain I don’t know how to implement it into my code because you don’t explain
this didn't help. mine just keeps going until it hits something and then stops. never goes back.
Hi Cynical,
Check the positions, pos1 and pos2.
If change direction only when hits something, it might be something with the pos1 and 2.
Share the script and I will be able to help you.
Thanks
@@xlaugts151 friend how can I put the double jump, since this video has been the only one that has worked for me to jump on the platforms and the player stays fixed on them ..
how do I double jump from the video code .. thanks
the moving platform suddenly stop, in the video click the Moving_Platform gameobject , go to the transform position set z to zero
It is work! Good Tutorial. Thanks you
why the script didnt work with me? i've do all.
its cool. thanks!!!
i did everything like in the video but i had 22 errors
lol
I need help
I can help you, what's the problem ?
great tutorials! if it's ok... as a part making 2d game, can you show how to make intro scene that the user ask to type his name so in the end of the level (in another scene) will be a massage "congratulations john you collect 5 coins".
Hi Shay,
I will have a look about that, but I am sure it's not complicated.
Thanks for the suggestion. :)
Just use playerprefs. It'll save the users name once the user finishes typing on the input field (can be customized) or you can tell them to press a button or hit enter when their done.
@@xlaugts151 how can i adapt that for a top down game? thx
@@se9979 yes of course you can my friend :)
Xlaugts how can I adapt it to a top down game? It's already moving, but I need my player to move with it, but your part for this is for a platformer?
thanks a lot!
tysm really helpful! :D
thanks, good video!
thank you :)
Very helpful thx !
Thank you for watching :)
Best Video, thanks
Thanks :)
Thank you
Thank you!
Mine just falls out the sky :s edit: it does work just not in the positions i told it to goes to random ones of its own accord. Edit again i fixed this by adding the script and collider to the parent instead of the child. I'm pretty sure you actually said to do this also xD also remember to turn gravity off
public class MoveSnail : MonoBehaviour
{
public float speed = 1f;
public int flip_cheak = 1; // если значение 1 объект переворачивается
public Transform pos1, pos2, startPos;
Vector2 nextPos;
private void Start()
{
nextPos = startPos.position;
}
void Update()
{
if (transform.position== pos1.position)
{
nextPos = pos2.position;
Flip();
}
if (transform.position == pos2.position)
{
nextPos = pos1.position;
Flip();
}
transform.position = Vector2.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
void Flip() // функция переворачивающая объект
{
if (flip_cheak == 1)
{
transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
}
}
}
THE CODE FROM VIDEO
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class platform : MonoBehaviour
{
public Transform pos1, pos2;
public float speed;
public Transform StartPos;
Vector3 nextPos;
void Start()
{
nextPos = StartPos.position;
}
// Update is called once per frame
void Update()
{
if (transform.position == pos1.position)
{
nextPos = pos2.position;
}
if (transform.position == pos2.position)
{
nextPos = pos1.position;
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
private void OnDrawGizmos()
{
// Gizmos.DrawLine(pos1.position, pos2.position);
}
}
Hey thanks
Hey, Thank you!!
Nice
Thanks Rameo
Didn't work for me :/
Thanks you
my platform is going down
👍
thank U 🌷
This is not a good tutorial. Your code is bad.
You are checking whether or not the platform's position is exactly equal to pos1 or pos2.
In 99% of cases, the platform will never be at exactly that position. Since you're using MoveTowards, the platform will overshoot its destination, and then overshoot it by the same amount in the other direction, endlessly.
@Hermes Belmont I literally did
@Hermes Belmont i mean they kinda did
It’s a vector 3 u can’t use >
thank you