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 Razion · Aug 01, 2012 at 08:15 AM · movementvectortranslation

Vector-based movement?

I have a script that handles rotation and movement for a character rigidbody. It rotates properly, and moves correctly in one direction. I'm using Input.GetAxis("Horizontal") to determine if the character should move left or right, but when applied to a Translation, the character ALWAYS moves in the positive direction, even when the input is negative. Any ideas?

 void FixedUpdate(){
 // LEFT/RIGHT ROTATION
 Vector3 dir = new Vector3(transform.position.x + (Input.GetAxis("Horizontal") * 5), transform.position.y, transform.position.z);
 Quaternion targetRotation = Quaternion.LookRotation(new Vector3(0, 0, Input.GetAxis ("Horizontal")));
 transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, Time.deltaTime * 10.0f);
 
 float translation = Input.GetAxis ("Horizontal") * moveSpeed * Time.deltaTime;
 Debug.Log (translation);
 transform.Translate (new Vector3(translation, 0, 0));

}

Comment
Add comment
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

1 Reply

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

Answer by senad · Aug 01, 2012 at 10:47 AM

You use the same input values for rotation and movement at the same time. Is that intentional?

Might it be that your game object gets rotated in such a way that the movement will always happen in only one direction? Try commenting out the rotation code and see what happens to the translation.

Comment
Add comment · Show 8 · 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 Razion · Aug 01, 2012 at 11:26 AM 0
Share

Yeah, that was the problem. Both of those things are dependent on the same input axis though. Any ideas how I could go about getting them to play nicely with each other?

avatar image senad · Aug 01, 2012 at 11:38 AM 1
Share

It depends on what you want to do. So what do you want to do??

If you want your moveSpeed to be constant you can do something like this:


float translation = Vector3.forward * moveSpeed * Time.deltaTime;

If you want it to stay dependent on the input axis range, you can just take the absolute of that value. Something alont the lines of:


$$anonymous$$ath.abs( Input.GetAxis ("Horizontal") )

But in either case, the sign of your translation should be always the same. (positive or negative)

avatar image Razion · Aug 01, 2012 at 07:45 PM 0
Share

That's the problem, though. Using an absolute value for either translation or rotation will only allow movement/rotation in one direction.

When horizontal = 1, I want to rotate to face right (which works), and when it's -1, I want to rotate to face left (which also works).

When I attempt to add translation (negative for left, positive for right), I always move to the right because my rotation is throwing me off. (This is a sidescroller)

Using Vector3.forward isn't an option as far as I know, because my rotation takes .5 seconds to go from 0 to 1 or -1 for the sake of animation purposes.

avatar image senad · Aug 02, 2012 at 08:37 AM 0
Share

Yes, so as I understand it, your rotation and translations add up and let your char move in the same direction each time.

So what I think you should do is always use positive translation values and leave the rotation as it is.

$$anonymous$$aybe I am understanding you wrong??

avatar image senad · Aug 02, 2012 at 09:24 AM 1
Share

I see. :)

If it is a side scroller, all you want to do is move your char left or right in world space. There is no need to use local model space.

Just add the parameter "Space.World" to your transform.Translate() call. Then the rotation will have no effect on your translation.

:)

Show more comments

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I translate my ship left and right and have it rotate at the same time? 1 Answer

Getting a 2d Sprite to move over time to an Array 1 Answer

How do I make an Enemy charge through a point? 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Make an object move to a given point by rotating to the correct direction first. 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