So whenever I press Shift, the Sprint Input Button, the animation plays, yet the WalkAcceleration stays the same. I’m pretty sure I’m missing something or maybe a line is overlapping another line.
Animation Script:
var AnimController : GameObject;
var rSound: AudioSource;
var rLoading: boolean = false;
var rLoadingAnim: Animation;
var Sprinting : Animation;
function Start() {
// Set all animations to loop
AnimController.animation.wrapMode = WrapMode.Loop;
// except shooting
AnimController.animation["Reload"].wrapMode = WrapMode.Once;
// Put idle and walk into lower layers (The default layer is always 0)
// This will do two things
// - Since shoot and idle/walk are in different layers they will not affect
// each other's playback when calling CrossFade.
// - Since shoot is in a higher layer, the animation will replace idle/walk
// animations when faded in.
AnimController.animation["Reload"].layer = 1;
// Stop animations that are already playing
//(In case user forgot to disable play automatically)
AnimController.animation.Stop();
}
function Update() {
if (Input.GetButton("Sprint") && Input.GetButton("Vertical")) {
animation.CrossFade("Run", 0.4);
PlayerMovement.walkAcceleration = 11005;
}
else {
animation.CrossFade("Idle", 0.5);
PlayerMovement.walkAcceleration = 5000;
}
if (Input.GetKey("w") && !Sprinting.IsPlaying("Run")) {
animation.CrossFade("Walk", 0.2);
}
if (Input.GetKey("a") && !Sprinting.IsPlaying("Run")) {
animation.CrossFade("LeftWalk", 0.2);
}
if (Input.GetKey("d") && !Sprinting.IsPlaying("Run")) {
animation.CrossFade("RightWalk", 0.2);
}
if (Input.GetKey("s") && !Sprinting.IsPlaying("Run")) {
animation.CrossFade("Backwards", 0.2);
}
if (Input.GetButtonDown("Reload1") && !rLoading && GunScript.currentAmmo < 36) {
animation.CrossFade("Reload", 0.2);
rSound.Play();
GunScript.currentAmmo = GunScript.maxAmmo;
rLoading = true;
}
if (rLoading && !rLoadingAnim.IsPlaying("Reload"))
{
rLoading = false;
}
};
PlayerMovement Script:
static var walkAcceleration: float = 5000;
var walkAccelAirRatio: float = 0.1;
var walkDeacceleration: float = 5;
@HideInInspector
var walkDeaccelerationVolx: float;
@HideInInspector
var walkDeaccelerationVolz: float;
var cameraObject: GameObject;
var maxWalkSpeed: float = 10;
@HideInInspector
var horizontalMovement: Vector2;
var jumpVelocity: float = 20;
@HideInInspector
var grounded: boolean = false;
var maxSlope: float = 60;
@HideInInspector
var paused : boolean = false;
function Update () {
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
}
if(grounded)
{
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
}
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio * Time.deltaTime);
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(Input.GetButtonDown("Jump") && grounded)
{
rigidbody.AddForce(0,jumpVelocity,0);
}
}
function OnCollisionStay (collision:Collision) {
for(var contact: ContactPoint in collision.contacts) {
if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope) {
grounded = true;
}
}
}
function OnCollisionExit () {
grounded = false;
}