I’m trying to make a charater controller using a rigidbody. What I’m tying to do is make my character walk to the right non stop.
here’s my code :
#pragma strict
class RunMovement
{
public var runPermited : boolean;
public var runSpeed : float;
@System.NonSerialized
public var runDirection : Vector3;
}
public var runMove : RunMovement;
function FixedUpdate ()
{
Run ();
}
function Run ()
{
if (runMove.runPermited)
{
rigidbody.AddForce (Vector3.right * runMove.runSpeed);
}
}
my character does move to the right, but having trouble with the speed. at first it move slowly but then the speed seems to multiply so my character move faster and faster every second. How do I set the runSpeed to not max the speed that I put in my var.
So are you using a character controller or a rigidbody? You dont need both for basic movement. Choose one. I would suggest character controller and the move function.
put your addForce in an if statement and test rigidbody.velocity.magnitute to see if the magnitude goes over a certain speed. So the force will only be added if the velocity is over a certain speed. You can also control the speed not to go faster than a certain speed.
So the code will look something like this: Sorry, I haven’t coded in javascript for a while, but hte code should be more or less
1.#pragma strict
2.
3.class RunMovement
4.{
5. public var runPermited : boolean;
6. public var runSpeed : float;
7.
8. @System.NonSerialized
9. public var runDirection : Vector3;
10.}
11.public var runMove : RunMovement;
12.
13.function FixedUpdate ()
14.{
15. Run ();
16.}
17.
18.function Run ()
19.{
20. if (runMove.runPermited rigidbody.velocity.magnitude > maxSpeed)
21. {
22. rigidbody.AddForce (Vector3.right * runMove.runSpeed);
23. }
24.}
you need to run a print to test how fast (in magnitude) you want your character to go. Then, let’s say it is 10, you can set maxSpeed to 10 and prevent the force from being added if the rigidbody moves faster than that. got it?
true what penguin is saying and it will be less expensive than the other way also. And you can still create acceleration if that is why you first used the addforce.
in c# it will look (something) like this, can’t remember unity script syntax…
transform.position += new Vector3(time.DeltaTime * speed,0,0);
im not sure if you can set the velocity property directly in javascript. If you can, and it works, yes. if you can’t you will have to set the posirion property.
Actually, chubbspet, if you’re using a rigidbody you’re meant to avoid setting the position directly because it messes with the physics. The rigidbody class has its own position variable/property (can’t remember which) if you need to do that.
But, yes, setting velocity directly is a pretty standard thing to do. The documentation says it should be avoided, but that’s because it makes things look unrealistic. If you’re not going for realism it’s not a problem.
#pragma strict
class RunMovement
{
public var runPermited : boolean;
public var runSpeed : float;
@System.NonSerialized
public var direction : Vector3;
}
public var runMove : RunMovement;
class JumpMovement
{
public var jumpPermited : boolean;
public var jumpGrounded : boolean;
public var jumpHeight : float;
}
public var jumpMove : JumpMovement;
function FixedUpdate ()
{
Run ();
Jump ();
}
function Run ()
{
if (runMove.runPermited)
{
runMove.direction = transform.right * Time.deltaTime * runMove.runSpeed;
transform.Translate (runMove.direction);
}
}
function Jump ()
{
if (Physics.Raycast (transform.position, -transform.up, 2))
{
jumpMove.jumpGrounded = true;
}
else
{
jumpMove.jumpGrounded = false;
}
if (jumpMove.jumpPermited)
{
if (Input.GetKey (KeyCode.UpArrow) jumpMove.jumpGrounded)
{
rigidbody.velocity = Vector3.up * jumpMove.jumpHeight;
}
}
}
for the run movement I used transform.Translate and for the jump I used rigidbody.velocity. One thing, how do I make my characer fall a bit faster after jump. Right now my character jump and fall kindda slow. Do i set my mass higher or? because if I set my mass higher my character cannot move at all.
Mass doesn’t change falling velocity. You can change Physics.gravity if you want to speed it up universally, you can add a ConstantForce component in the direction of gravity just for your character, you can do it in your script by modifying his velocity, and there’s probably other options.
You probably shouldn’t use transform.Translate(…) to move your character if there’s a Rigidbody involved, because it’ll mess with the physics calculations. Check to see if the Rigidbody has its own equivalent (I’ve got a feeling there’s rigidbody.Move(…) or something like that).