- Home /
My character needs to teleport back before he can float, how do I fix that?
So currently, hitting/holding Z teleports him back to where he started the level at, then he floats from there, no matter where hes at in the level, he always teleports back to that spot then he floats. He is supposed to be able to float no matter where his current position is in the level.
The Z key is not actually part of the start method, its in its own section
void Start()
{
rb = GetComponent<Rigidbody2D>();
//Set a starting velocity when our Game Starts (Set x to 1 for moving Right and -1 for moving Left)
Vector3 DefaultVelocity = new Vector3(1 * spead, 0, 0);
rb.velocity = DefaultVelocity;
moveDir = spead;
originalPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
rend = GetComponent<Renderer>();
rend.enabled = true;
//THIS IS IN OUR FLOATING SECTION
tempPoisition = transform.position; //uses the transform in the object's inspector. position uses x, ys and z. to use the current position. so no matter where it starts, it will always be correct
//CODING THE Z KEY TO FLOAT
if ((Input.GetKey(KeyCode.RightArrow)) && isGrounded && Input.GetKey(KeyCode.Z))
{
tempPoisition.x += horizontalSpeed;
tempPoisition.y += verticalSpeed = Mathf.Sin(Time.realtimeSinceStartup * verticalSpeed) * amplitude;
transform.position = tempPoisition; //apply x and y to current position
Answer by Zaeran · Dec 31, 2020 at 07:58 PM
You have an equals in your tempPosition.y += line, so your y value is most likely being set directly to your Sin function.
Chaging it to a + should help.
After doing that, there's a red underline under the whole thing here:
tempPoisition.y + verticalSpeed
Ahh, wrong equals sign :) The one after verticalSpeed
tempPoisition.y += verticalSpeed = $$anonymous$$athf.Sin(Time.realtimeSinceStartup verticalSpeed) amplitude;
I think im close. So I kept it mostly with how it was in the OP, except I moved tempPoisition = transform.position; from start to fixed updated. that fixed the teleporting issue, but a new issue is he never gets in the air to float, so its like hes running instead lol.
Idk how to fix that
Your answer
Follow this Question
Related Questions
Disable teleporter after being used 1 Answer
How to: shoot bullets from an rotating object 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiate random platforms 0 Answers