Character Clipping Through Walls When Uncrouching and Receiving Errors

I have been attempting to fix one of my scripts to make sure that my character does not clip through walls when the player unchrouches, or in this scenario, lets go of “C.” I tried adding a Raycast that I turned from Javascript into C#, but, this is giving me quite a few errors. These errors are:

  1. Assets/Scripts/Crouching/RunAndCrouch.cs(50,68): error CS0165: Use of unassigned local variable `hit’
  2. Assets/Scripts/Crouching/RunAndCrouch.cs(50,36): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit, float)’ has some invalid arguments
  3. Assets/Scripts/Crouching/RunAndCrouch.cs(50,36): error CS1620: Argument #3' is missing out’ modifier

I am attempting to make the script send a test message “Hit” into the console when an object is above the character, but the script returns these errors and prevents me from playing the scene. Here is the script:

using UnityEngine;
using System.Collections;

public class RunAndCrouch : MonoBehaviour
public float walkSpeed = 7; // regular speed
public float crchSpeed = 3; // crouching speed
public float runSpeed = 20; // run speed
[SerializeField] private float _standHeight = 0.5f;
[SerializeField] private float _radius = 0.5f;
[SerializeField] private LayerMask _terrianLayers;

private CharacterMotor chMotor;
private Transform tr;
private float dist; // distance to ground
private bool CanStand()
	return Physics.CheckCapsule(transform.position, transform.position + new Vector3(0f, _standHeight, 0f), _radius,

// Use this for initialization
void Start () 
	chMotor =  GetComponent<CharacterMotor>();
	tr = transform;
	CharacterController ch = GetComponent<CharacterController>();
	dist = ch.height/2; // calculate distance to ground

// Update is called once per frame
void FixedUpdate ()
	float vScale = 1.0f;
	float speed = walkSpeed;
	if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
		speed = runSpeed;            
	if (Input.GetKey("c"))
	{ // press C to crouch
		vScale = _standHeight;
		speed = crchSpeed; // slow down when crouching
		var up= transform.TransformDirection(Vector3.up);
		RaycastHit hit;    
		Debug.DrawRay(transform.position, up * 1.1f,;
		if(Physics.Raycast(transform.position, up, hit, 1.1f)){

	chMotor.movement.maxForwardSpeed = speed; // set max speed
	float ultScale = tr.localScale.y; // crouch/stand up smoothly 
	Vector3 tmpScale = tr.localScale;
	Vector3 tmpPosition = tr.position;
	tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
	tr.localScale = tmpScale;
	tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position        
	tr.position = tmpPosition;

I am not sure how I would go about fixing these errors or if there is a different way to go about fixing my character clipping through things when it crouches. Can anyone possibly help me fix these errors or find another solution?

On line 38 add the keyword “out”

if(Physics.Raycast(transform.position, up, out hit, 1.1f)){

Okay, thank you both. I managed to fix the errors, and I have figured out how to stop the character from clipping through walls.