Getting Y-Axis And Setting It In Unity?

Started by
8 comments, last by Nypyren 8 years, 5 months ago

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

What will you make?
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);

Visit http://www.mugsgames.com

Stroids, a retro style mini-game for Windows PC. http://barryskellern.itch.io/stroids

Mugs Games on Twitter: [twitter]MugsGames[/twitter] and Facebook: www.facebook.com/mugsgames

Me on Twitter [twitter]BarrySkellern[/twitter]

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

What will you make?

Could you post the script in question?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

What will you make?
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?

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!

What will you make?

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

What will you make?

Normalize the move direction before you apply the speed.

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();

This topic is closed to new replies.

Advertisement