# Unity Getting Y-Axis And Setting It In Unity?

This topic is 1126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey,

So I'm making a pretty simple RPG in Unity, but can't seem to find how to get the value of the y-axis. I have an open square platform (temporary) and want to be able to read the y-axis value, and set it to 2.7 if it reaches a certain point (say -7), along with setting the x-axis value to 0 (So you don't spawn on empty space and fall, to be stuck in a loop). This would mean falling off the platform doesn't require having to restart the game.

I appreciate any help. I'm new to Unity, but not to programming, or C#

Thanks,

Ovi

##### Share on other sites

Every game object has a transform, which contains its position, which is a 3D vector. The y position can be read from that. Is that what you mean? So, in your Update function, something along the lines of: if (transform.position.y < -7) transform.position = new vector3(0, 2.7f, 0);

Edited by BarrySkellern

##### Share on other sites

Every game object has a transform, which contains its position, which is a 3D vector. The y position can be read from that. Is that what you mean? So, in your Update function, something along the lines of: if (transform.position.y < -7) transform.position = new vector3(0, 2.7f, 0);

It compiled, but doesn't seem to actually work. I ran the game, dropped off the platform, and did not get reset to the position in the vector3 (left it running a good 30 seconds).

EDIT: Script: http://pastebin.com/jN2mNHCW

Edited by Ovicior

##### Share on other sites

Could you post the script in question?

##### Share on other sites

Could you post the script in question?

http://pastebin.com/jN2mNHCW

Sorry, I had done it, and forget to paste the link into the post.

##### Share on other sites
Try putting the position check outside the isGrounded if block, or in an else block for that if. If you're falling, you're not on the ground, right? Edited by Nypyren

##### Share on other sites

Try putting the position check outside the isGrounded if block, or in an else block for that if. If you're falling, you're not on the ground, right?

Thank you so much! It works now! I didn't read the entire block -_- Lesson learned!

##### Share on other sites

Could you post the script in question?

Didn't want to make another thread for this so... If you hold down two keys (Like S & D to move diagonally down) you move twice as fast due to the speed having two keys' worth of force added to it. How can I limit this? Here's one thing I tried, but it doesn't appear to be working. https://github.com/Ovicior/Life-RPG/blob/master/PlayerController.cs

##### Share on other sites

Normalize the move direction before you apply the speed.

##### Share on other sites
Since the Input.GetAxis functions return values from -1 to +1 on each axis, the easiest solution would be to normalize your Vector3 between lines 19 and 20, but ONLY if its length is already greater than 1, because you don't want to force vectors slower than the speed limit to be forced UP to the speed limit (which can happen if you don't press any key, or when you start using a joystick). And we can use sqrMagnitude since it's slightly more efficient and still works in this case (anything less than 1 squared is still less than 1, 1 squared is 1, and anything greater than 1 squared is greater than 1).

http://docs.unity3d.com/ScriptReference/Vector3-sqrMagnitude.html
http://docs.unity3d.com/ScriptReference/Vector3.Normalize.html

if (moveDirection.sqrMagnitude > 1.0f)
moveDirection.Normalize();

Edited by Nypyren

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013393
×