- Home /
2D Top-Down Movement: Transform.up and right don't change
I've been working on a top-down 2D RPG and trying to figure out how to fire a projectile or swing a sword in the same direction that the player is facing. I'm using a RigidBody2D attached to the player to move it around the world using addForce.
I'm trying to determine the direction my player is facing using transform variables (e.g. transform.right) but they always show the same values.
**playerScript.cs: https://github.com/bdickason/stormsword/blob/weapons/Assets/scripts/characters/PlayerScript.cs**
private MoveScript moveScript;
// Speed of the player
public Vector2 speed = new Vector2(50, 50);
// Retrieve axis information from controller
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// Calculate movement per-direction
moveScript.direction = new Vector2(inputX, inputY);
**moveScript.cs: https://github.com/bdickason/stormsword/blob/weapons/Assets/scripts/characters/MoveScript.cs**
// Speed of object
public float speed = 400;
// Direction of object
public Vector2 direction = new Vector2(-1, 0);
// Actual movement
private Vector2 movement = new Vector2(0, 0);
// Update is called once per frame
void Update () {
/* Check if character is moving */
movement = direction * speed; // Calculate movement amount
}
void FixedUpdate() {
// Apply the movement to the rigidbody
rigidbody2D.AddForce (movement);
}
This allows me to move my character around the screen easily.
Based on dozens of other Answers on this site, as the player moves around, I should be able to access the player's transform.right or transform.up to determine which direction the player is facing.
Unfortunately, I always get the same results when debugging or using Debug.Log for both of the above variables (as well as transform.forward).
transform.up: (0, 1.0, 0) transform.right: (1.0, 0, 0) transform.forward: (0, 0, 1.0)
In my code, I'm trying to access this information from my weaponScript.cs, but I've tried debugging the values on the playerScript itself (which should access the player's transform) as well as the rigidBody2d object's transform and I still get the same, unchanging results depicted above.
The project is available for download here: https://github.com/bdickason/stormsword/tree/weapons
Any help in determining where my character is facing and why my rigidbody does not change direction would be appreciated!
(Note: I've also tried a velocity-based approach by using rigidbody2D.velocity with the same results)
Update: If I create a vector called 'facing' and update it only when the character is receiving input data, I can deter$$anonymous$$e the direction the character is facing, but this seems like a hack when I have plenty of tools built into the RigidBody2D to do this.
Answer by Pyrian · Jul 10, 2014 at 12:42 AM
You don't appear to do any rotating of the Player, you just have a direction of movement. So, if by facing you simply mean the direction of movement, all you need is the velocity vector:
Vector2 MyFacing = rigidbody2D.velocity.normalized;
You can take that vector, multiply it by a speed float value, and assign it to the velocity of a projectile.
.up, .right, .forward, and so on, are static constants. They don't have anything whatsoever to do with the vector they're called on (in fact you really shouldn't call them on a specific vector at all); you can't use them to get information out.
Thanks I'll try that out this evening!
To be clear - you still have to store it in a vector because velocity will revert to 0 when the player stops moving.
For anyone who stumbles on this answer, I've used the variable Pyrian suggested to check if the character changes velocity (if velocity.x or velocity.y > 0) and then store it in a temporary vector called 'facing.'
This vector's x and y are then used when calculating projectile direction and sent to my Animator as facing_x and facing_y for animations.
Here's the code:
Store 'facing' vector for each character: https://github.com/bdickason/stormsword/blob/f9daa966f3a64b2efe6d6f522168378f0956d898/Assets/scripts/characters/$$anonymous$$oveScript.cs
Grab 'facing' vector when creating a new projectile: https://github.com/bdickason/stormsword/blob/f9daa966f3a64b2efe6d6f522168378f0956d898/Assets/scripts/weapons/WeaponScript.cs#L47
Answer by cyberlarry · Jul 09, 2014 at 11:11 PM
I have the same problem :(. If i will find the solution i will share with you mate.
I'm currently using the 'direction' variable on my character to deter$$anonymous$$e which direction he/she is facing. If at any time the character is moving (direction.x or direction.y != 0), it stores that in a 2D vector, rounded to 1, or -1 in any direction. This ensures that we will always get either (1,0), (0,1), etc. (and never 0,0).
I also had to write a simple rounding function because the built in c# rounding libraries don't let you always round away from zero (i.e. round 0.9 to 1.0 or -0.9 to -1.0).
Here's the code:https://github.com/bdickason/stormsword/blob/eea913db59fdaf6a8aa606395640abe4d498ff76/Assets/scripts/characters/$$anonymous$$oveScript.cs
I may update it to use the velocity variable from the rigidbody2D that Pyrian suggested below to see if that works any better.
Your answer
Follow this Question
Related Questions
Megaman Movement 0 Answers
Movement of an prefab without rigidbody? 1 Answer
Make characters go through each other 1 Answer