- Home /
Problem is not reproducible or outdated
Character Jumping while Sprinting Code?
When my character Jumps while Sprinting he goes the same distance X wise as he does when he jumps while walking. Is there anywhere i can put code to have my character jump farther while running?
var walkSpeed: float = 7; // regular speed var crchSpeed: float = 3; // crouching speed var runSpeed: float = 20; // run speed private var chMotor: CharacterMotor; private var tr: Transform; private var dist: float; // distance to ground function Start(){ chMotor = GetComponent(CharacterMotor); tr = transform; var ch:CharacterController = GetComponent(CharacterController); dist = ch.height/2; // calculate distance to ground } function Update(){ var vScale = 1.0; var speed = walkSpeed; if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){ speed = runSpeed; } if (Input.GetKey("c")){ // press C to crouch vScale = 0.5; speed = crchSpeed; // slow down when crouching } chMotor.movement.maxForwardSpeed = speed; // set max speed var ultScale = tr.localScale.y; // crouch/stand up smoothly tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime); tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position }
Please format your code properly in your question, otherwise you won't find many people interested to read through your code. If your code is properly formatted in $$anonymous$$onoDevelop or VS, then click the "Code Sample" icon in the toolbar above your question (5th from the left), and then copy that code and paste it inside the "Code Sample" window. This way it won't lose it's original formatting.
Answer by M-G-Production · Dec 05, 2014 at 11:20 PM
I formatted your code, and there is nothing here about Jumping :S
var walkSpeed: float = 7;
// regular speed
var crchSpeed: float = 3;
// crouching speed
var runSpeed: float = 20;
// run speed
private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float;
// distance to ground
function Start()
{
chMotor = GetComponent(CharacterMotor);
tr = transform;
var ch:CharacterController = GetComponent(CharacterController);
dist = ch.height/2;
// calculate distance to ground
}
function Update()
{
var vScale = 1.0; var speed = walkSpeed;
if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift"))
{speed = runSpeed;}
if (Input.GetKey("c"))
{
// press C to crouch
vScale = 0.5;
speed = crchSpeed;
// slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed;
// set max speed
var ultScale = tr.localScale.y;
// crouch/stand up smoothly
tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale);
// fix vertical position
}
Can you share CharacterMotor with us? I think the problem is in the Jump function.
Bests, Math