Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Dec 27, 2016 at 04:34 PM by JeffHardddyyy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JeffHardddyyy · Dec 25, 2016 at 05:44 PM · c#transformvector3input.getkey

Set new default position each time?

First, I know it's messy code, just bear with me, this is the first project that I was trying to avoid a lot of research so it's a "Try what I know" kinda thing.

Second, So I have this bit of code :

    void Update()
     {
 
         if (Input.GetKey(KeyCode.W))
         {//If "W" Is Pressed, Go Forward .10f
             gameObject.transform.position += new Vector3(0, 0, 0.10f);
         }
 
         if (Input.GetKey(KeyCode.UpArrow))
         {//If "Up Arrow" Is Pressed, Go Forward .10f
             gameObject.transform.position += new Vector3(0, 0, 0.10f);
         }
 
 
 
         if (Input.GetKey(KeyCode.A))
         {//If "A" Is Pressed, Go Left .10f
             gameObject.transform.position += new Vector3(-0.10f, 0, 0.10f);
         }
 
         if (Input.GetKey(KeyCode.LeftArrow))
         {//If "Left Arrow" Is Pressed, Go Left .10f
             gameObject.transform.position += new Vector3(-0.10f, 0, 0);
         }
 
 
 
 
 
         if (Input.GetKey(KeyCode.S))
         {//If "S" Is Pressed, Go Backwards .10f
             gameObject.transform.position += new Vector3(0, 0, -0.10f);
         }
 
         if (Input.GetKey(KeyCode.DownArrow))
         {//If "Down Arrow" Is Pressed, Go Backwards .10f
             gameObject.transform.position += new Vector3(0, 0, -0.10f);
         }
 
 
 
 
 
 
 
 
         if (Input.GetKey(KeyCode.D))
         {//If "D" Is Pressed, Go Right .10f
             gameObject.transform.position += new Vector3(0.10f, 0, 0);
         }
 
         if (Input.GetKey(KeyCode.RightArrow))
         {//If "Right Arrow" Is Pressed, Go Right .10f
             gameObject.transform.position += new Vector3(0.10f, 0, 0);
         }

}

Now if you run that, there are no bugs, except if you were to run it, then rotate whatever it's attatched to, to something like 180 degrees, when trying to get forward, it will go backwards. (Because it's in reverse due to 180)

I was just curious if there is anyway to make it so whenever you press the "UpArrow" // "W" it will always go forward.

Thanks in advance, I hope this doesn't go under "Asks for script"; Also have a great day & Merry Christmas :)

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

  • Sort: 
avatar image
0
Best Answer

Answer by UnityCoach · Dec 25, 2016 at 07:12 PM

You can use transform.Translate () instead of assigning the position property. It has an option for Space, use Space.Self, and it will always move in its own local coordinates.

Merry Christmas!

Comment
Add comment · Show 16 · 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 JeffHardddyyy · Dec 26, 2016 at 05:10 PM 0
Share

So would I do gameObject.transform.translate += new Vector3 (0.10f, 0, 0);

etc?

avatar image UnityCoach JeffHardddyyy · Dec 26, 2016 at 05:27 PM 0
Share

you don't need to specify gameObject, transform is am inherited property of $$anonymous$$onoBehaviour. Translate is a method of the Transform component. You don't assign it a value, you pass it parameters. Something in the like :

 transform.Translate(Vector3.up * 0.1f, Space.Self);

Check out the doc here

avatar image JeffHardddyyy UnityCoach · Dec 26, 2016 at 05:33 PM 0
Share

But what would you do if you don't want it to go up?

I tried : transform.Translate(Vector3.up * 0.1f, 0, Space.Self);

And I was bombarded with errors...

Show more comments
avatar image UnityCoach JeffHardddyyy · Dec 26, 2016 at 05:29 PM 0
Share

You also want to check out the Input class. When you want to take input, it's much better to use this than hardcoded keys.

avatar image JeffHardddyyy UnityCoach · Dec 26, 2016 at 05:38 PM 0
Share

$$anonymous$$inda confused now...

Show more comments
avatar image JeffHardddyyy · Dec 26, 2016 at 05:48 PM 0
Share

Is there any docs that make it able to go slower and to make it so you can only make it go up a certain amount?

avatar image UnityCoach JeffHardddyyy · Dec 26, 2016 at 09:38 PM 0
Share

$$anonymous$$athf.Clamp allows you to clamp a value between $$anonymous$$ and max values. You can multiply by Time.deltatime to have a framerate independent speed, and multiply the result by a speed multiplier float value that you declare public and that you can adjust in the inspector.

avatar image JeffHardddyyy · Dec 26, 2016 at 10:17 PM 0
Share

I want it so if you rotate it, it will still go forward on "W" / "Up_Arrow" ? Not a different direction..

avatar image UnityCoach JeffHardddyyy · Dec 26, 2016 at 10:22 PM 0
Share

No offense, you should take a quick training course on program$$anonymous$$g fundamentals.

This is a one liner :

 void Update()
 {
     transform.Translate (Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0, Space.Self);
 }
avatar image UnityCoach UnityCoach · Dec 26, 2016 at 10:24 PM 0
Share

or if you're in a 3D project.

 void Update()
 {
     transform.Translate (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"), Space.Self);
 }
Show more comments
avatar image JeffHardddyyy · Dec 27, 2016 at 04:34 PM 0
Share

Aye, I'm sorry! This was all on me, When you did "Vector.up" I always kept telling myself "It's always gonna go forward 1, which you added "* 0.1f" which I took it as "It's gonna go forward 1.01" Because I was dumb for a whole day, Lol.

I apologize, when I went to go look it up, it was what I wanted, This was on me, You were right, I was wrong. Sorry for the problem.

Follow this Question

Answers Answers and Comments

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

Related Questions

Can’t keep the position 0 Answers

(26,47): error CS1525: Unexpected symbol `)' 1 Answer

Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer

How to properly split string received from server to update Vector3? 2 Answers

Need help with this code? thanks. 2 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