• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alex-Tabor · Oct 08, 2016 at 07:13 PM · vector3transform.positionfixedupdateupdate problemadding

transform.position not adding correctly with a Vector3

Hello Community,
I'm trying to change the y and z axis of my player at the same time as the player move up and down. Problem is at first only the z axis is getting added and the y remains the same.
The following is in a function being called in my FixedUpdate() function. The player will move and if I look at my inspector, after I stop, the y and z are the same.
I am using the z axis later to help me calculate a jump as the z stays where the ground is and the y is where the player is but the code returns this same issue.

     Debug.Log("vertical: " + vertical);
     movement.Set(horizontal, vertical, vertical); // z axis is to place which character is in front
     
     movement = movement.normalized * speed * Time.deltaTime; // make sure player isn't passing max speed and doesn't move per frame
     Debug.Log("movement: " + movement);
     Debug.Log("player's first position: " + transform.position);
     transform.position += movement; // Move the player
     Debug.Log("player's updated position: " + transform.position);
  

For the first movement I do the Log Shows:
vertical: 1
movement: (0.0, 0.1, 0.1)
player's first position: (-2.5, 0.0, 0.0)
player's updated position: (-2.5, 0.0, 0.1)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image voncarp · Oct 08, 2016 at 08:20 PM 0
Share

Often you would want to use y for vertical movement, z for forward and back, and x for side movement.

You have two axis set as vertical which is likely going to move you up/down and forward/back at the same time. Probably not what your expecting.

If you can show a bit more code, I may be able to get this for you. movement.Set doesnt look right.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by voncarp · Oct 09, 2016 at 01:28 AM

Hard to tell what you are trying to do here. Is this for for 2D or 3D movement?

It looks like you need to do this:

2D movement.Set(horizontal, vertical, 0); // z axis is to place which character is in front

3D movement.Set(horizontal, 0, vertical); // z axis is to place which character is in front

You would change that 0 to whatever your jump functionality is to get the desired jump effect.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alex-Tabor · Oct 10, 2016 at 05:16 PM 0
Share

A game such as Streets of Rage. The z axis is moving the characters in front of each other.

An example without it"movement.Set(horizontal, vertical, 0);": Say character A is standing behind character B and character A looks like it is behind. When character A goes in front of character B, it will still look like character A is behind just down lower, so just their feet stick out below.

"movement.Set(horizontal, 0, vertical); " will just keep the character from moving up and down.

avatar image tanoshimi Alex-Tabor · Oct 10, 2016 at 05:26 PM 0
Share

That's normally done by changing the sorting layer of the sprite based on their y-position in screenspace, not based on player input.

avatar image Alex-Tabor tanoshimi · Oct 11, 2016 at 06:33 PM 0
Share

Thanks for pointing this out to me. It doesn't explain why Unity isn't able to spit out the correct Vector data, but is definitely helpful for what I am trying to accomplish. I think I can make it work with another variable that holds the Y location of the ground below the character and use that ins$$anonymous$$d of the character's y. Do you know if two objects can share the same sorting order within a sorting layer?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to find total distance from start to end of an array which stores positions in vector3? 2 Answers

Updating transform.position correctly to instantiated GameObjects 0 Answers

Trouble converting transform.position to C# 1 Answer

Y position keeps changing, after action is complete? 1 Answer

How to get a proper ramming effect? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges