Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bdickason · Jul 05, 2014 at 06:57 PM · 2dmovementrigidbodyinput

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)

Comment
Add comment · Show 1
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 bdickason · Jul 05, 2014 at 04:53 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · 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 bdickason · Jul 10, 2014 at 09:02 AM 0
Share

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.

avatar image bdickason · Jul 10, 2014 at 09:14 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 1 · 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 bdickason · Jul 10, 2014 at 09:01 AM 0
Share

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

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

Megaman Movement 0 Answers

Movement of an prefab without rigidbody? 1 Answer

Make characters go through each other 1 Answer

My controls are inverted when facing sideways. 1 Answer

Unwanted 2d pixel after-image when moving 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