I created a simple code as a controller for a fps and then after trying to add a sprint function I kept geting this error:
Assets/Script/_Playermove.cs(51,17): error CS0266: Cannot implicitly convert type double' to
float’. An explicit conversion exists (are you missing a cast?)
and I do not know what this means. Here is the code:
using UnityEngine;
using System.Collections;
public class _Playermove : MonoBehaviour {
public float movementspeed = 3.0f;
public float mousesensitivity = 5.0f;
public float jumpspeed = 3.0f;
float pitchrot = 0;
public float pitchlimit = 90.0f;
float verticalvelocity = 0;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
}
// Update is called once per frame
void Update () {
CharacterController cc = GetComponent<CharacterController>();
//rotation
float rotyaw = Input.GetAxis("Mouse X") * mousesensitivity;
transform.Rotate(0, rotyaw, 0);
float rotpitch = Input.GetAxis("Mouse Y") * mousesensitivity;
pitchrot-=Input.GetAxis("Mouse Y") * mousesensitivity;
pitchrot = Mathf.Clamp(pitchrot, -pitchlimit, pitchlimit);
Camera.main.transform.localRotation = Quaternion.Euler(pitchrot, 0, 0);
//Movement
float forwardspeed = Input.GetAxis("Vertical") * movementspeed;
float sidespeed = Input.GetAxis("Horizontal") * movementspeed;
if(cc.isGrounded && Input.GetButton("Jump") ) {
verticalvelocity = jumpspeed;
}
if(Input.GetKeyDown(KeyCode.LeftShift) ) {
movementspeed = 3;
}
if(Input.GetKeyUp(KeyCode.LeftShift) ) {
movementspeed = 5;
}
verticalvelocity += -9.81 * Time.deltaTime;
Vector3 speed = new Vector3( sidespeed, verticalvelocity, forwardspeed );
speed = transform.rotation * speed;
cc.Move( speed * Time.deltaTime );
}
}