Zone
July 16, 2011, 7:53pm
1
Hello, all. I have two problems.
The first being, what I need help most on; I would like my character to constantly move right, along the X-axis. that way the player can only jump on platforms.
var groundSpeed : float; //Force applied when grounded
var airSpeed : float; //Force applied when in air
var jumpSpeed : float; //Force applied when jumping
var maxSpeed : float; //Max speed used for limiting Rigidbody’s lateral speed
var limitSpeed : boolean; //Determines whether or not speed limit should be applied
var onGroundDrag : float; //Rigidbody drag used on ground
var inAirDrag : float; //Rigidbody drag used in air
private var onGround : boolean = false; //Determines whether the player is grounded or in air
private var jump : boolean = false; //Determines whether the player should jump
private var rb : Rigidbody; //Reference to the rigidbody component of the game object
private var t : Transform; //Reference to the transform component of the game object
function Awake() {
rb = rigidbody; //Cache rigidbody component
t = transform; //Cache transform component
}
//Check for one-off input in the Update loop since checking for input in FixedUpdate can lead to lost input events
function Update() {
if(Input.GetKeyDown(KeyCode.Space)) {
if(onGround) {
jump = true; //The player will jump when next FixedUpdate is called
}
}
}
//Apply physics forces in FixedUpdate
function FixedUpdate() {
rb.AddForce(Input.GetAxisRaw(“Horizontal”) * t.right * ((onGround) ? groundSpeed : airSpeed)); //Add force in the direction of movement
if(jump) {
rb.AddForce(t.up * jumpSpeed); //Add upwards force to make player jump
onGround = false; //Player is no longer grounded
jump = false;
rb.drag = inAirDrag; //Change rigidbody drag to allow for better movement in air
}
}
//Apply post-physics limits in LateUpdate
function LateUpdate() {
if(limitSpeed Mathf.Abs(rb.velocity.x) > maxSpeed) //If limitSpeed is set to true
rb.velocity.x = Mathf.Sign(rb.velocity.x) * maxSpeed; //Limit rigidbody’s x speed
}
//Check for collisions to determine when the player is on the ground
function OnCollisionEnter(collision : Collision) {
var collisionAngle = Vector3.Angle(collision.contacts[0].normal, Vector3.up); //Calculate angle between the up vector and the collision contact’s normal
if(collisionAngle < 40.0) { //If the angle difference is small enough, accept collision as hitting the ground
onGround = true; //Player is grounded
rb.drag = onGroundDrag; //Restore original drag value
}
}
The second, less important thing, is when my character gets to close to a platform and jumps it gets stuck. Im figuring it has to do with Drag, or Friction.
Thanks in advance.
-Justin.
zine92
July 17, 2011, 12:04am
2
Use the code tag to wrap your codes. Go Advanced and press the # button. Why don’t you use character controller? It’s more simpler for simpler stuff like basic movement jumping and stuff. Unity - Scripting API: CharacterController
to move along the x axis you do this
var speed = 10;
function Update () {
transform.Translate(speed*Time.deltaTime,0,0);
}