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
1
Question by gamezdragon · Oct 23, 2014 at 01:49 AM · c#transformtimeforwardsame

How to combine movements of the transform.rotation and transform.position

So, I'm wondering what to do for a code like this in C#:

 using System.Collections;
 
 public class Movement : MonoBehaviour {
     public float speed = 0.45f;
     public float turnSpeed = 20f;
 
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.UpArrow)){
             animation.CrossFade ("Walk");
             transform.position += transform.forward * speed;
         }
         
         else if (Input.GetKey(KeyCode.RightArrow)){
             animation.CrossFade ("Walk");
             transform.Rotate(Vector3.up * turnSpeed);
         }
         
         else if (Input.GetKey(KeyCode.LeftArrow)){
             animation.CrossFade ("Walk");
             transform.Rotate(Vector3.down * turnSpeed);
         }
         
         else{
             animation.CrossFade("Idle");
         }
     
     }
 }


So what do I do if I want to combine the movements of the rotations and the forward transform.position? For example, when I press the up arrow, the character walks forward, and when I press one of the right/left arrows the character turns around, however if I press the up arrow and the left/right arrows at the same time, only one happens (it either turns in place or walks forward)

So my question is, how do I code it so the character can walk forward while turning, and later walk backwards while turning, doing both at the same time without having to continuously just press the side arrows and the up arrows separately? This is so I can have smoother controls, obviously.

Thanks for reading!

Comment
Add comment · Show 2
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 Habitablaba · Oct 23, 2014 at 02:02 AM 0
Share

Just use a bunch of if statements ins$$anonymous$$d of if/else if/else

avatar image gamezdragon · Oct 23, 2014 at 02:04 AM 0
Share

What do you mean? I used to work with JS, so I'm newer to C#. In Java, you had to always add an 'else' or you couldn't add multiple 'ifs'.

So if you just use 'if' one doesn't override the other?

1 Reply

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

Answer by Bunny83 · Oct 23, 2014 at 02:06 AM

Uhm, just remove the "else" from your if statements so each if statement is checked regardless of the one before. However you have to handle your pure else differently:

 void Update()
 {
     bool moving = false;
     if (Input.GetKey(KeyCode.UpArrow)){
         moving = true;
         animation.CrossFade ("Walk");
         transform.position += transform.forward * speed;
     }
     if (Input.GetKey(KeyCode.RightArrow)){
         moving = true;
         animation.CrossFade ("Walk");
         transform.Rotate(Vector3.up * turnSpeed);
     }
     if (Input.GetKey(KeyCode.LeftArrow)){
         moving = true;
         animation.CrossFade ("Walk");
         transform.Rotate(Vector3.down * turnSpeed);
     }
     
     if (!moving)
     {
         animation.CrossFade("Idle");
     }
 }
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 gamezdragon · Oct 23, 2014 at 02:11 AM 0
Share

It almost works, however the walk animation finishes, freezes, and idle doesn't play again. It only plays at the start.

avatar image Bunny83 · Oct 23, 2014 at 02:15 AM 0
Share

Are you sure you have the moving boolean declared inside Update? What you describe would happen when you declare it as class member variable which it shouldn't be.

I'll edit my answer to make that more clear ;)

The variable (which is just a local variable on the stack) is always initialized as false. If no movement happens it will be still false when you reach the if(!moving) statement. However if any (or multiple) of your other if statements execute it will be set to true.

avatar image gamezdragon · Oct 23, 2014 at 02:16 AM 0
Share

Ahhh yes it's a variable, not in Update! Thanks! I'll try it out. :)

edit -- It worked, thanks!

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

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

Related Questions

I want a better explination than the one in unitys tutorial 2 Answers

Move from one position to another in x seconds 2 Answers

Camera transformation upon a pressing a key 1 Answer

Double reference to waypoints in FSM 0 Answers

Truck moves sideways when using transform.forward,When i translate my truck with transform.forward it goes sideways 0 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