Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

avatar image Alex-Tabor voncarp · Oct 08, 2016 at 08:58 PM 0
Share

Here's all of the two functions and the declaration of the movement.

Vector3 movement; // Direction player is moving in

 // Physics update
 void FixedUpdate()
 {
     // Get the horizontal and vertical change
     // Raw to be -1, 0 or 1 to snap to full speed
     // "Horizontal" and "Vertical" are defaults in unity
     float horizontal = Input.GetAxis("Horizontal");
     float vertical = Input.GetAxis("Vertical");

     $$anonymous$$ove(horizontal, vertical); // move the player

     // if player is jumping
     if(jumping)
     {
         Jump();
     }
 }
 // $$anonymous$$ove the player
 void $$anonymous$$ove(float horizontal, float vertical)
 {
     // if player is about to go out of bounds
     if(vertical > 0 && this.transform.position.y >= y$$anonymous$$ax || vertical < 0 && this.transform.position.y <= y$$anonymous$$in)
     {
         vertical = 0;
     }
     if(horizontal > 0 && this.transform.position.x >= (Camera.main.transform.position.x + x$$anonymous$$ax) || horizontal < 0 && this.transform.position.x <= (Camera.main.transform.position.x + x$$anonymous$$in))
     {
         horizontal = 0;
     }
     
     // If player has changed direction
     if(horizontal > 0 && lastHoizontal < 0 || horizontal < 0 && lastHoizontal > 0)
     {
         Flip(); // Change the direction the player is facing
         lastHoizontal = horizontal; // Update current direction
     }
     Debug.Log("vertical: " + vertical);
     movement.Set(horizontal, vertical, vertical); // z axis is to place which character is in front
     Debug.Log("y: " + movement.y);
     Debug.Log("z: " + movement.z);
     movement = movement.normalized * speed * Time.deltaTime; // make sure player isn't passing max speed and dosn't move per frame
     Debug.Log("movment: " + movement);
     Debug.Log("player's first position: " + transform.position);
     transform.position += movement; // $$anonymous$$ove the player
     Debug.Log("player's updated position: " + transform.position);
 }

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Place object in different positions when L is clicked 1 Answer

How do I get one object to move to (the closet) another object? 2 Answers

trouble with smooth damping 0 Answers

Turn and Move Foward 1 Unit relative to Objects local direction 1 Answer

Getting Vector3 in between two given Vector3 after given Distance. 2 Answers


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