Jump to content
  • Advertisement
Sign in to follow this  
Ovicior

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to 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 this post


Link to 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?

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

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!