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 Coreyf716 · Sep 26, 2012 at 11:27 PM · animationjavascripttransformtranslatekeycode

Player Transformation

While working with the character controls, I noticed a problem. When two keys that translate the player in opposite directions are pressed at the same time, the player does not move. If one is already being held and the other is pressed, the player stops.

How can I get the player to continue in the same direction the player was originally moving before the second key is pressed, but not move when both are pressed at once and the player is not moving?

This is the code.

 function Start () {
 
 animation["F1WalkForward"].layer = 2;
 
 animation["F1WalkBack"].layer = 2;
 
 animation["F1WalkLeft"].layer = 1;
 
 animation["F1WalkRight"].layer = 1;
 
 }
 
 var FootStepSound : AudioClip;
 
 var JumpSound : AudioClip;
 
 var MoveAmount = 0.15;
 
 var JumpAmount = 250;
 
 function Update () {
 
 if (Input.GetKey(KeyCode.W)) {
 
 transform.Translate (Vector3.forward * MoveAmount);
 
 audio.clip = FootStepSound;
 
 audio.Play();
 
 animation.Play("F1WalkForward");
 
 }
 
 if (Input.GetKey(KeyCode.S)) {
 
 transform.Translate (Vector3.back * MoveAmount);
 
 audio.clip = FootStepSound;
 
 audio.Play();
 
 animation.Play("F1WalkBack");
 
 }
 
 if (Input.GetKey(KeyCode.A)) {
 
 transform.Translate (Vector3.left * MoveAmount);
 
 audio.clip = FootStepSound;
 
 audio.Play();
 
 animation.Play("F1WalkLeft");
 
 animation.Stop("F1WalkForward");
 
 animation.Stop("F1WalkBack");
 
 }
 
 if (Input.GetKey(KeyCode.D)) {
 
 transform.Translate (Vector3.right * MoveAmount);
 
 audio.clip = FootStepSound;
 
 audio.Play();
 
 animation.Play("F1WalkRight");
 
 animation.Stop("F1WalkForward");
 
 animation.Stop("F1WalkBack");
 
 }
 
 }

For example, when the A & D keys are pressed at the same time, I do not want the player to move. When the A key is currently being held and the D key is pressed, I do not want the player to stop translating in the direction of the A key.

Also, how can I loop the foot steps sound only while one of the keys are being held?

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

2 Replies

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

Answer by CaioRosisca · Sep 28, 2012 at 06:37 PM

Unfortunatelly I can´t test it here, but that should work. Your if() should be like that:

 if (Input.GetKey(KeyCode.W)) {
     isForward = true;
 
     if(!isBack){//!isBack is the same as isBack == false
         transform.Translate (Vector3.forward * MoveAmount);
     }
 }
 
 else {
     isForward = false;
 
 }

 if (Input.GetKey(KeyCode.S)) {
     isBack = true;
     
     if(!isForward){
         transform.Translate (Vector3.back * MoveAmount);
     }
 }
 
 else {
     isBack = false;
 }

Try it again using this piece of code.

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 Coreyf716 · Sep 29, 2012 at 12:19 AM 0
Share

It worked. Thanks for all of the help man :)

avatar image CaioRosisca · Sep 29, 2012 at 04:21 AM 0
Share

@Coreyf716 if your question is solved, please upVote my answer and mark it as the correct one :)

avatar image
0

Answer by CaioRosisca · Sep 27, 2012 at 12:06 AM

For me that´s exactly how it should work and I see absolutely no reason for doing this, but each case is a case. So here we go, what you could try is, inside the if() that moves the player to the right you could also check if the player wasn´t already moving left(), if it was, simply return.

For example:

 if (Input.GetKey(KeyCode.D)) {
     if(Input.GetKey(KeyCode.A))
          return;
     
     transform.Translate (Vector3.right * MoveAmount);
     
     audio.clip = FootStepSound;
     
     audio.Play();
     
     animation.Play("F1WalkRight");
     
     animation.Stop("F1WalkForward");
     
     animation.Stop("F1WalkBack");
     
 }

Then do this for each key.

Comment
Add comment · Show 3 · 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 Coreyf716 · Sep 27, 2012 at 12:47 AM 0
Share

This didn't do anything.

avatar image CaioRosisca · Sep 27, 2012 at 03:35 AM 0
Share

Hmm, you could try to set booleans for each move direction, like isLeft, isRight..when u pless the relative key set them to true, and when moving to the oposite direction try to do the same as above, but with an else..inside if(key.D) put is(isLeft){return} else {all the moving right code}..\

make sure that when you are not pressing the key the boolean is set back to false

avatar image Coreyf716 · Sep 27, 2012 at 10:26 PM 0
Share

I'm using this script.

function Start () {

}

var isForward : boolean = false;

var isBack : boolean = false;

var is Left : boolean = false;

var isRight : boolean = false;

var $$anonymous$$oveAmount = 0.15;

function Update () {

if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) {

transform.Translate (Vector3.forward * $$anonymous$$oveAmount);

isForward = true;

}

else {

isForward = false;

}

if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) {

transform.Translate (Vector3.back * $$anonymous$$oveAmount);

isBack = true;

}

else {

isBack = false;

}

}

}

This way, isForward is only active while W is being held. I've tried if (isForward == true) {

} but it didn't work. I also tried if(isRight == true && isLeft == true) {

} but when this statement is attached to both keys, it does the same as it would without the statement.

Is it possible to cancel a statement if another key is being held?

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

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

Is it possible to continue a method after Input? 2 Answers

Moving Animations 3 Answers

Transform.Translate change distance and speed 1 Answer

How to translate object one movement with script 1 Answer

Rotating An Object On Its Axis Once 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