- Home /
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);
}
}
}
}
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).
Yep, not to mention you are adding a forward force on literally every FIxedUpdate frame, so I would Imagine it accelerates pretty fast.
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
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.
Your answer
Follow this Question
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