Problem with Crouch Script - 3D C#
I cant seem to work out what the problem with my script is, here it is below:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {
public float movementSpeed = 5.0f;
public float mouseSensitivity = 5.0f;
public float jumpSpeed = 20.0f;
float verticalRotation = 0;
public float upDownRange = 60.0f;
float verticalVelocity = 0;
CharacterController characterController;
//Crouching
private float crouchHeight;
private float standardHeight;
private bool crouching = false;
private GameObject mainCamera;
void Start () {
//Cursor.lockState = CursorLockMode.Locked;
characterController = GetComponent<CharacterController>();
mainCamera = GameObject.FindGameObjectWithTag ("MainCamera");
standardHeight = characterController.height;
crouchHeight = (characterController.height / 2);
crouching = false;
}
void Update () {
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if( characterController.isGrounded && Input.GetButton("Jump") ) {
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3( sideSpeed, verticalVelocity, forwardSpeed );
speed = transform.rotation * speed;
characterController.Move( speed * Time.deltaTime );
if (Input.GetKey (KeyCode.LeftShift)) {
if (crouching) {
characterController.height = standardHeight;
characterController.center = new Vector3 (0, 0, 0);
mainCamera.transform.localPosition += Vector3(0, crouchHeight, 0);
crouching = false;
return;
}
if (!crouching) {
Crouch ();
}
}
}
void Crouch() {
characterController.height = crouchHeight;
characterController.center = new Vector3 (0, -0.5, 0);
mainCamera.transform.localPosition.y -= Vector3(0, crouchHeight, 0);
crouching = true;
}
}
Here are the errors that come up: Assets/FirstPersonController.cs(62,71): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Assets/FirstPersonController.cs(75,70): error CS1502: The best overloaded method match for UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments Assets/FirstPersonController.cs(75,70): error CS1503: Argument
#2' cannot convert double' expression to type
float'
Assets/FirstPersonController.cs(76,57): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
If anyone could help, I would be very grateful :D
Answer by Zeoli · Mar 27, 2016 at 04:33 PM
On line 75, put an f after - 0.5 to denote a float otherwise it is a double. I. E. - 0.5f
Answer by Jaidan · Mar 27, 2016 at 04:40 PM
Thank you! , that sorted out two of the errors, I just have these two left:
Assets/FirstPersonController.cs(62,71): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Assets/FirstPersonController.cs(76,57): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
These usually mean you have a type ins$$anonymous$$d of the variable you intend. Try creating a vector3 variable on the line before the ones refered to and pass them ins$$anonymous$$d of new vector3().