- Home /
About move way and Speed limit....
hello,everyone. I creat a move script for my protagonist.
var MousePoAlpha : Vector2;
var MousePoOmega : Vector2;
static var moveYN : boolean = true;
function Update () {
if(transform.position.z != 0){ transform.position.z=0; }
if(moveYN==true){ transform.rotation = Quaternion.LookRotation(rigidbody.velocity); if(Input.GetMouseButtonDown(0)){ MousePoAlpha=Input.mousePosition; }
if(Input.GetMouseButtonUp(0)){
MousePoOmega=Input.mousePosition;
rigidbody.velocity=Vector3(MousePoOmega.x-MousePoAlpha.x,MousePoOmega.y-MousePoAlpha.y,0)/5;
}
rigidbody.velocity=rigidbody.velocity*0.999; // Slow deceleration.
}
if(moveYN==false&&Input.GetMouseButtonUp(0)){ transform.rigidbody.velocity =transform.TransformDirection( Vector3( 0, 20, 0) ); }
}
This is a 2DGame. I drog my protagonist to a direction and it will go.
My teacher say this is not good way to move. He say we need affiliate a "time". But I don't kown where we need affiliate this "time".
And it have a problem when speed was too high... It will through other Object!(No collision)
Because this is a phone game so I'm not willing to consume too many resources. So I think limit speed is a good way.
Have any API can limit speed about velocity?
Thank you.
Answer by save · May 16, 2011 at 10:28 AM
Physics only compute at a certain speed, when velocities gets to high it cannot keep up with computing the colliders around that particular rigidbody.
In our game we use something similar to this to control maximum speed:
var ShipSpeed = 0.0; var Acceleration = 10; var MaxSpeed = 100;
function Start() {
rigidbody.drag = 3;
rigidbody.angularDrag = 10;
}
function FixedUpdate() {
ShipSpeed = ShipSpeed + Acceleration*Time.deltaTime;
if(ShipSpeed >= MaxSpeed){ShipSpeed=MaxSpeed;}
var gas : float = Input.GetAxis ("Accelerate") * ShipSpeed;
rigidbody.AddRelativeForce (Vector3.forward * gas);
}
and let the drag do the deceleration. It really depends on what kind of game it is tho. If you're dealing with physics it should be inside a FixedUpdate.
But won't your code run differently on different machines? If my machince can run each FixedUpdate at 2ms and yours takes 4ms, the car will accelerate fast on my machine, even with FixedUpdate. I think you still need a Time.deltaTime if you want the "feel" of your game to be the same across platforms (i.e. acceleration at the same rate, etc.)...
Very good point, I have to look into it thanks. I've always assumed that FixedUpdate() was fixed to a static time to compute the same physics over all platforms.
Added time as a variable to the script. Remember to always use FixedUpdate when dealing with physics over time.
Answer by cjmarsh · May 16, 2011 at 10:11 AM
I think the problem is that the velocity is being multiplied by a constant scalar every frame, resulting in variable acceleration (depending on frame rate). If you introduce a variable to represent the magnitude of change to your velocity and then multiply it by Time.deltaTime you will get an actual acceleration that should obey the laws of physics once more.