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 NewLife Prod. · Sep 02, 2013 at 06:52 PM · transforminputplayerdirectionpostion

transforming player position on input.get button down

Hello, game developers. May someone help me out please. I am making a 3d side scroller for pc and I want to know how to change the way my player faces by pushing the left or right arrow key or a,d keys. I have my player input settings set as Vertical for left being the negative btn and right as the postive btn. What happens is that when I Press either the left or right key the player transforms to one side. Here is my script.

 #pragma strict
 var jumpSound : AudioClip;
 var walkSound : AudioClip;
 
 var soundPlaying : boolean = false;
 
 var playerPosition : Transform;
 
 function Start () {
 
 }
 
 function Update () {
     if (Input.GetButtonDown ("Vertical")&& soundPlaying == false){
         audio.PlayOneShot (walkSound);
              soundPlaying = true;
      
         playerPosition.transform.rotation.x += 180;
         playerPosition.transform.rotation.y = 0;
         playerPosition.transform.rotation.z += 0;
         
      
     }
     
 
 
     if (Input.GetButtonDown ("Jump")){
         audio.PlayOneShot (jumpSound);
     
     }
     
     if (Input.GetButtonUp ("Vertical")){
         audio.Stop ();
               soundPlaying = false;
               
     
     
     }
     
 
 }
 
 @script RequireComponent (AudioSource)


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 gamerant · Sep 02, 2013 at 08:39 PM 0
Share

Well "Vertical" is for up and down movement so ins$$anonymous$$d of confusing yourself and everyone else try using "Horizontal".

Here is a slightly modified code for you that rotates the player around.

But it only works once.And honestly I have no idea why, so you will have to figure out yourself how to make it work all the time.Or maybe someone here could point out how to do it and show me where I made a mistake :D

 function Update () 
 {
     if (Input.GetButton ("Horizontal"))
     {
        playerPosition.transform.rotation.x += 0 * Time.deltaTime;
        playerPosition.transform.rotation.y += 180 * Time.deltaTime;
        playerPosition.transform.rotation.z += 0 * Time.deltaTime;
  
     }
 }

1 Reply

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

Answer by ArkaneX · Sep 02, 2013 at 08:22 PM

Please take a look at Input.GetAxis. Reading axis value will tell you which key was pressed. For "Vertical" axis, value greater than 0 is up, and value lower than 0 is down. For "Horizontal" axis positive value is right and negative is left.

Depending on the value returned, you need to either rotate left or right, but don't do this using x,y,z properties of rotation (Quaternion) - use Transform.Rotate method instead. For example, to rotate around Y axis:

 if (Input.GetButtonDown("Horizontal"))
 {
     if (Input.GetAxis("Horizontal") > 0)
     {
         playerPosition.transform.Rotate(0, 90, 0);
         Debug.Log("right");
     }
     else
     {
         playerPosition.transform.Rotate(0, -90, 0);
         Debug.Log("left");
     }
 }

If required, remember to rotate back after key is released.

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 NewLife Prod. · Sep 02, 2013 at 09:09 PM 0
Share

Thank you very much!

avatar image gamerant · Sep 02, 2013 at 11:23 PM 1
Share

Don't forget to mark it as answered otherwise it's always open and people will keep looking at this post.

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with the movement of the player 1 Answer

How to detect two keys released simultaneously [Input System] 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

make player move in direction it's facing 2 Answers

Use GameObject's global position instead of local position 3 Answers


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