How to save Position data in other variables
Hi there,
I have been working on a very simple game that only requires a SPACEBAR to opporate and make the player jump from platform to platform.
What I have done (at the moment) is making 3 platforms slide to left when the player is in the air and stop sliding to the left when the player is touching the ground again (aka the next platform). I noticed that after each and every jump the platforms would not go to a full round number, which all have a distance of 7 on the X axis.
In the code down here I was able to make the platform SNAP to -7 if the player landed on a number higher than that. I do need to add that the first platform starts at 0x,0y and the last at -14x,0y:
public class PlatformController : MonoBehaviour
{
private PlayerMovement playerMovement;
private GameObject thePlatforms;
private float scrollSpeed = 0;
// Start is called before the first frame update
void Start()
{
playerMovement = GameObject.FindObjectOfType<PlayerMovement>();
thePlatforms = GameObject.Find("The Platforms");
}
// Update is called once per frame
void Update()
{
if (playerMovement.isGrounded == false)
{
//Scrolling
GameObject currentChild;
for (int i = 0; i < transform.childCount; i++)
{
currentChild = transform.GetChild(i).gameObject;
scrollSpeed = 9f;
PlatformScroller(currentChild);
PlatformLocatie();
}
}
}
void PlatformScroller(GameObject currentScroller)
{
currentScroller.transform.position -= Vector3.right * (scrollSpeed * Time.deltaTime);
}
void PlatformLocatie()
{
if (thePlatforms.transform.position.x <= -7f)
{
thePlatforms.transform.position = new Vector2(-7, 0);
}
}
}
For extra clearity I will also give you my Hiarchy,
SampleScene (called world)
- Main Camera
- Player
- PlatformScroller (Which has this script onto it)
- - The Platforms (collective of all 3 platforms)
- - - Rode Platform (I am dutch Rode, Rood means Red)
- - - Groene Platform (Groene, Groen means Green)
- - - Blauwe Platform (Blauwe, Blauw mean Blue)
What I want is a variable or a command that saves the position data of the last jump and makes it the new current position.
So the player starts at 0,0 > jumps > new position is -7,0 > -7,0 is stored > jumps > new position is -14,0 > -14,0 is stored > jumps > new position is back at 0,0 (the platform will loop)
What happens now is that the player will jump from 0,0 just fine to -7,0 but will never continue since it is lower than -7,0.
thanks in advance and kind regards,
Kellin
Your answer
Follow this Question
Related Questions
Moving an array of Vector3 with the GameObject (Path-System) 0 Answers
transform.Translate changing object position by incorrect amount 1 Answer
C# code understanding problem 2 Answers
Randomly set an integer as positive or negative? 1 Answer
What is the correct way to learn unity scripting ? 2 Answers