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 IvekoTurboo · Nov 29, 2021 at 05:31 PM · 3rd person cameraaccelerate

Speed Limit,Sideways movement and speed limit

Hey yall so im new to the game making scene and im making my first game 3d game. Its a basic square moving game but it constantly gains speed. Heres the code im using so if anyone can put like a speed limit there it be so helpfull. Thanks in advance!

using UnityEngine;

public class PlsyerMovement : MonoBehaviour{

 public Rigidbody rb;

 public float forwardForce = 2000f;
 public float sideawaysForce = 500f;
 public float backwardsForce = 500f;

 // Update is called once per frame
 void FixedUpdate ()
 {
     // Add a forward force
     rb.AddForce(0, 0, forwardForce * Time.deltaTime); 

     if ( Input.GetKey("d") )
     {
  
         rb.AddForce(sideawaysForce * Time.deltaTime, 0, 0);
     }
     if ( Input.GetKey("a") )
     {
  
         rb.AddForce(-sideawaysForce * Time.deltaTime, 0, 0);
       if ( Input.GetKey("s") )
     {
  
         rb.AddForce(backwardsForce * Time.deltaTime, 0, 0);
     }        
     }
 }
 

} ,Hey so im new to programming and im making a basic 3d square game and was wondering how to put a speed limit for the cube to not accelerate and to go be able to go sideways. This is the code im using and if anyone can put like a speed limit in the code it be very helpfull. Thanks in advance!

using UnityEngine;

public class PlsyerMovement : MonoBehaviour{

 public Rigidbody rb;

 public float forwardForce = 2000f;
 public float sideawaysForce = 500f;
 public float backwardsForce = 500f;

 // Update is called once per frame
 void FixedUpdate ()
 {
     // Add a forward force
     rb.AddForce(0, 0, forwardForce * Time.deltaTime); 

     if ( Input.GetKey("d") )
     {
  
         rb.AddForce(sideawaysForce * Time.deltaTime, 0, 0);
     }
     if ( Input.GetKey("a") )
     {
  
         rb.AddForce(-sideawaysForce * Time.deltaTime, 0, 0);
       if ( Input.GetKey("s") )
     {
  
         rb.AddForce(backwardsForce * Time.deltaTime, 0, 0);
     }        
     }
 }
 

}

Comment
Add comment · Show 4
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 CodesCove · Nov 30, 2021 at 02:12 PM 0
Share

NOTE! all your key presses accelerate the object in X axis.. So when pressing S it will be same effect as pressing D. You actually never will go backwards.

Anyway if you still need speed limit then overall speed limit is the simplest:

Add this before FixedUpdate()

 public float speedLimit = 100; // or what ever..

Add this before AddForce:

 if(rb.velocity.magnitude > speedLimit) return;

If you want speed limit to certain direction then you need to compare the velocity vectors corresponding direction value to the speedlimit. Length of the rb.velocity vector gives you the overall speed (magnitude).

avatar image sacredgeometry CodesCove · Dec 01, 2021 at 05:46 AM 0
Share

Yep, not to mention you are adding a forward force on literally every FIxedUpdate frame, so I would Imagine it accelerates pretty fast.

avatar image CodesCove sacredgeometry · Dec 01, 2021 at 08:45 AM 0
Share

oh right, so move the speed limit check to after adding forward force. or just keep constant forward speed with rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, forwardSpeed). forwardSpeed being class level float variable

avatar image sacredgeometry · Dec 01, 2021 at 05:50 AM 0
Share

As a side note there is no advantage to shorter less descriptive variable names all you are doing is confusing future you or other people.

Dont shorten, acronym or abreviate names unless you really need to or there is a strong language convention (Which is rarely). Its really bad coding style.

0 Replies

· Add your reply
  • Sort: 

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

131 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

Related Questions

Make Camera follow to Object(3rd person Camera) 1 Answer

3rd person camera-based player movement 0 Answers

Modifying Cinemachine values during runtime 1 Answer

Applying accleration to character controller without pressing keys 0 Answers

how to view char from side at all times with 3rd person controller 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