- Home /
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)
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.
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);
}
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.
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.
That's normally done by changing the sorting layer of the sprite based on their y-position in screenspace, not based on player input.
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
![](https://koobas.hobune.stream/wayback/20220612094242im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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