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 DovahkiinHero · Jun 16, 2015 at 05:44 AM · movementvector3

'Moving' a fixed Distance to the left/right

I want to create a Game, where your Character has to Dodge blocks etc. on 3 Diffrent Lanes(f.e. like Subway Surfer) I just cant figure out how to set a fixed Distance for my Character to move. (Note that my Character will be constantly moving forward). I have setup a Level, where you spawn on a huge cube(0,0,0). How can i tell Unity to move my Character EXACTLY 30 to X?

 public class burgerController : MonoBehaviour
     {
     // speed is set on 5 in the Editor.
     public float speed;
     public int counter = 0;
      
 void Update()
 {

  transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.World);
 
  if (counter >= 0)
  {
      if (Input.GetKeyDown("a"))
      {
          transform.Translate(new Vector3(-30, 0, 0) * speed * Time.deltaTime, Space.World);
          counter--;
      }
  }
  if (counter <= 0)
  {
      if (Input.GetKeyDown("d"))
      {
          transform.Translate(new Vector3(30, 0 , 0) * speed * Time.deltaTime,Space.World);
          counter++;
      }
  }
  if (counter == -1)
  {
      if (Input.GetKeyDown("s"))
      {
          transform.Translate(new Vector3(30, 0, 0) * speed * Time.deltaTime, Space.World);
          counter = 0;
      }
  }
  if (counter == 1)
  {
      if (Input.GetKeyDown("s"))
      {
          transform.Translate(new Vector3(-30, 0, 0) * speed * Time.deltaTime, Space.World);
          counter = 0;
      }
  }

}

I have tried several Ways to fix this and just couldnt find a Solution, it is really Important and i would appreciate your help. (my Character is just a sphere btw. (1/1/1 Scale) everything is setup really simple in my Level)

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

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

Answer by skalev · Jun 16, 2015 at 06:07 AM

If you want to move an object a fixed distance, you need to remove the multiplication with Time.deltaTime & speed. That multiplication makes the motion dependent on the speed and time that has passed in the frame.

transform.Translate(new Vector3(-30, 0, 0), Space.World); will move the object -30 units along the world X axis. Note that this will the motion on a single frame.

Comment
Add comment · Show 1 · 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 DovahkiinHero · Jun 16, 2015 at 07:22 AM 0
Share

Thank You SO $$anonymous$$UCH! You dont believe how much you helped me with this! Cant believe i didnt realize my $$anonymous$$istake myself because it makes absolutly no sense! Im just a beginner, and this was a huge fix for me, thank you!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

CharacterController, Move toward mouse 1 Answer

Move Obj A towards Obj B BUT keep going in same direction when Obj A has reached Obj B 1 Answer

Increasing the speed of an object when the scale is decreased and vice versa 0 Answers

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Need helping getting a character controller to jump 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