Multiplyaer FPS sprint speed does not work
I HAVE THIS WARNING: Assets/Scripts/FirstPersonController.cs(65,23): warning CS0219: The variable `speed' is assigned but its value is never used I FOLLOWED THE MIKE GEIG TUTORIAL ABOUT MERRY FRAGMAS AND BECAUSE OF THIS WARNING I CAN'T SPRINT. THE ANIMATION WORK'S, BUT MY SPEED DO NOT INCREASE. HERE IS THE SCRIPT:
using UnityEngine; using System.Collections;
[RequireComponent (typeof(CharacterController))] public class FirstPersonController : MonoBehaviour {
public bool walkByDefault = true;
public float runSpeed = 8.0f;
public float movementSpeed = 5.0f;
public float mouseSensitivity = 6.0f;
public float upDownRange = 60.0f;
public float jumpSpeed = 20f;
float verticalVelocity = 0;
CharacterController characterController;
Animator anim;
// Use this for initialization
void Start ()
{
Screen.lockCursor = true;
characterController = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update ()
{
//Rotation
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotLeftRight, 0);
float rotUpDown = Input.GetAxis("Mouse Y") * mouseSensitivity;
float currentUpDown = Camera.main.transform.rotation.eulerAngles.x;
float desiredUpDown = currentUpDown - rotUpDown;
//desiredUpDown = Mathf.Clamp (desiredUpDown, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(desiredUpDown, 0, 0);
//Movement
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if ( characterController.isGrounded && Input.GetButtonDown ("Jump")) {
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3 ( sideSpeed, verticalVelocity, forwardSpeed );
speed = transform.rotation * speed;
characterController.Move ( speed * Time.deltaTime);
anim = GetComponentInChildren<Animator> ();
}
public void LateUpdate () {
float speed = runSpeed;
#if !MOBILE_INPUT
// On standalone builds, walk/run speed is modified by a key press.
// We select appropriate speed based on whether we're walking by default, and whether the walk/run toggle button is pressed:
bool walkOrRun = Input.GetKey(KeyCode.LeftShift);
bool aim =Input.GetButton ("Fire2");
if(aim)
walkOrRun =false;
speed = walkByDefault ? (walkOrRun ? runSpeed : movementSpeed) : (walkOrRun ? movementSpeed : runSpeed);
// On mobile, it's controlled in analogue fashion by the v input value, and therefore needs no special handling.
anim.SetBool ("Sprint" , walkOrRun);
anim.SetBool ("Aim", aim);
#endif
}
}
ANY IDEAS?
Answer by Glurth · Sep 13, 2015 at 10:21 PM
You check for walkOrRun, and adjust speed appropriately in LateUpdate()
float speed = runSpeed;
...
if(aim)
walkOrRun =false;
speed = walkByDefault ? (walkOrRun ? runSpeed : movementSpeed) : (walkOrRun ? movementSpeed : runSpeed);
but you do NOT do this in Update(), which is where you actually move the character.
Vector3 speed = new Vector3 ( sideSpeed, verticalVelocity, forwardSpeed );
speed = transform.rotation * speed;
//need to adjust for walk or run here
characterController.Move ( speed * Time.deltaTime);
Can you explain me simple, please? Because I do not understand very good what you say. I need to move some script lines or what?
You need to add some of the code the that you have in LateUpdate(), to your Update() function.
In paricular, you need to check if the character is running or walking: and based on that result, assign a speed(magnitude) of runSpeed or movementSpeed. I don't know if you want to adjust forwardSpeed& 'sideSpeed', or speed, that will be up to you.
Well I have followed the $$anonymous$$erry Fragmas tutorial and I write what the speaker said. Here is the tutorial: https://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/merry-fragmas-multiplayer-fps
Your answer