• 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
Question by Vandarthul · Dec 25, 2012 at 05:16 PM · objectcharactermovingleftright

Controlling if object is moving right or left

Hello, currently I'm trying to create a 2D platformer for some practice. I got character moving jumping shooting but I want to check if the character is moving right or left, so I can shoot my projectile accordingly. I'm working with C#, thanks in advance!

 public float movementSpeed;
 transform.Translate(Input.GetAxis("Horizontal") *movementSpeed * Time.deltaTime,0,0,Space.World);

By the way , I also want my character to interact with objects correctly( when I run towards an obstacle, my character is trying to go into the obstacle, I want $$anonymous$$m to stop when it collides with an obstacle) in my research people recommend to make velocity zero when you want your character to stop but I t$$anonymous$$nk "transform.translate" method has no relation with physics that's why it doesn't work for me, so can you give any other methods about moving my character?

Comment

People who like this

0 Show 5
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 Vandarthul · Dec 27, 2012 at 08:53 AM 0
Share

I hope I can get an answer soon because I really would like to develop myself in this community.

avatar image whydoidoit · Dec 27, 2012 at 08:54 AM 0
Share

Please bump questions by editing them or posting a comment (and not an "Answer") on UA Answer means Solution and not reply.

avatar image whydoidoit · Dec 27, 2012 at 08:54 AM 0
Share

I've converted your answer to a comment.

avatar image whydoidoit · Dec 27, 2012 at 08:55 AM 0
Share

Presumably you have some code that actually moves this character based on the players input - can you post that code perhaps? Hard to help you without knowing what you are doing.

avatar image AlucardJay · Dec 27, 2012 at 02:40 PM 0
Share

I want to check if the character is moving right or left : first store your Input in a variable, then check if that has a value of approximately +- 0.02 (you should include a deadzone for sensitive analogue inputs). Then you can still use that variable in your move command. eg :

 float deadZone = 0.02f;
 
 // in method
 float inputHorz = Input.GetAxis("Horizontal");
 if ( inputHorz < -deadZone  ) {
     // object going left
 }
 else if ( inputHorz > deadZone ) {
     // object going right
 }
 transform.Translate( inputHorz *movementSpeed * Time.deltaTime,0,0,Space.World);
 

want my character to interact with objects correctly are you using a character controller? If not, you'll need to use physics or some very clever obstacle detection. One relationship I noticed (and clear me up if I'm wrong) is that using rigidbody.velocity is pretty much the same as transform.position. eg :

 transform.position += transform.forward * speed * Time.deltaTime;
 
 rigidbody.velocity = transform.forward * speed;

For me this has worked very well, have very quickly added rigidbodies and modified scripts using this relationship.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

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

Related Questions

How do you make an object go left and right? 1 Answer

how do I create a character 2 Answers

How can my character stop moving immediately after it gets hit by enemy? 1 Answer

Rotate Sphere in the same place in Android touch 1 Answer

Object fly around another? 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