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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DovahkiinHero · Jun 14, 2015 at 02:02 PM · movement

My Code isnt 'always' working like it should, but 'sometimes' it does

I have a very simple running 'Game'. In the Game, you have 3 Lanes that you can move to. In my Code i have set the movement to a certain amount of Distance to the left, and right. That means, my Player will always transform to the same Positions. Now, while sometimes my Code works exactly the Way it should, there are cases (f.e. when i play the game on my Laptop, or even on my PC) where the Character moves too much to the left/right and then your Coordinates are ruined and you are moving outside of the Platform.

This is my Code:

 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 used an integer counter, so you cant fall off the 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
0

Answer by NoseKills · Jun 14, 2015 at 04:30 PM

In my Code i have set the movement to a certain amount of Distance to the left, and right. That means, my Player will always transform to the same Positions.

That's not what your code is doing :). In all of your Translate calls you have Time.deltaTime as a multiplier. That means the amount you are moving depends heavily on your framerate at the time you press a, s or d.

In your forward movement Translate call you have to use deltaTime, since you do that every frame and you need to move the character a different distance depending on framerate to make the movement smooth.

In your sideways movement code you do the movement instantly and only once when the button is pressed so you should just calculate exactly how much you need to move to left or right and not have any randomness like deltaTime in the parameters.

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 14, 2015 at 05:01 PM 0
Share

oh i thought i make the game work independently from the FPS when i put in Time.deltaTime? Could you give me an example how i should change my Code since im just a beginner

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces? 2 Answers

How to make the enemy stops according the player position 1 Answer

Tracking HTC Vive Controller Motion Direction? 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