- Home /
Custom C# Crouch Script
Hey,
I know there are some similar questions out there. I have already looked int to them but most of them are written in Java- respective Unityscript, with which I' m quiet unfamiliar. So I come up with this one.
To get used to the scripting with Unity, I try to work out erverthing by myself. I'm currently working trough a C# book at the same time, so potentally the problem is the poor understanding by myself for this language.
I came up with, like the title says, a charctercontroller script and tried to add a crouching ability. So this is the code I came up with (I erased the unnecessary code):
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float movementSpeed = 5.0f;
public float rotationSpeed = 5.0f;
float verticalRotation = 0;
float rotationBoundary = 60.0f;
public float jumpSpeed = 0.0f;
float verticalVelocity = 0;
float ccSizeCrouch = 2;
int crouchOffset = 1;
float cameraCrouchOffset = - 0.75f;
//Methoden Deklaration
// Use this for initialization
void Start ()
{
Screen.lockCursor = true;
}
// Update is called once per frame
void Update ()
{
//Kamera Drehung zum Umschauen
float lookHorizontal = Input.GetAxis ("Mouse X") ;
transform.Rotate (0, lookHorizontal, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") ;
verticalRotation = Mathf.Clamp (verticalRotation, -rotationBoundary, rotationBoundary);
//Speichert die Kamera Rotation als Quaterion Euler um sie mit Vector 3 verwenden zu können
Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
//Hier wird die Bewegung eingerichtet
CharacterController cc = GetComponent<CharacterController>();
//Ermöglicht dem Char zu springen
if (Input.GetButtonDown ("Jump") && cc.isGrounded)
{
verticalVelocity = jumpSpeed;
}
//Schwerkraft
verticalVelocity += Physics.gravity.y * Time.deltaTime;
//Grundlegende Bewegung auf den Achsen
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, verticalVelocity, moveVertical);
//Sprinten
if (Input.GetButton ("Run"))
{
movementSpeed = 10.0f;
}
else
movementSpeed = 5.0f;
//Setzt den neuen Transform in Abhänigkeit von Rotation
movement = transform.localRotation * movement;
cc.Move ((movement * movementSpeed)*Time.deltaTime) ;
Implementrierung von Crouch
if (Input.GetButton ("Fire3"))
{
CharacterController CrouchHeight = (cc.height/ccSizeCrouch) * Time.deltaTime ;
cc.transform.localPosition.y -= crouchOffset * Time.deltaTime;
Camera cameraCrouchposition = (camera.main.transform.localPosition.y - cameraCrouchOffset) * Time.deltaTime;
if (Input.GetButtonUp ("Fire3"))
{
CrouchHeight = (cc.height*ccSizeCrouch)*Time.deltaTime;
cc.transform.localPosition.y += cameraCrouchOffset * Time.deltaTime;
cameraCrouchposition = (camera.main.transform.localPosition.y + cameraCrouchOffset) * Time.deltaTime;
}
}
}
}
After I saved this one and switched back to Unity, I got some errors, which I wasn't able to get fixed.
I would appreciate your help and your opinion with that. And please excuse my spelling and language skills, I'm not a native speaker and it has passed some time since the last time I wrote english.
Answer by maccabbe · Mar 03, 2015 at 01:16 PM
At line 89 you cannot define a CharacterController as a float, CharacterController CrouchHeight = (cc.height/ccSizeCrouch) * Time.deltaTime ; Since CrouchHeight's value is not used anywhere else I'm not really sure what you were trying to do here.
At line 90 you attempt to modify the y coordinate of transform.position but transform.position uses get/set so you need to assign the whole thing. You probably want
cc.transform.localPosition -= new Vector3(0, crouchOffset * Time.deltaTime, 0);
At line 91 you cannot use camera.main as Camera.main is a static variable and must be gotten using Camera.main (C# is case sensitive). In addition, at line 91 you then try to assign a float to a variable of type camera. You might be trying to do the following
Camera.main.transform.localPosition -= new Vector3(0, cameraCrouchOffset* Time.deltaTime, 0);
The other 4 errors are the same as 1-3 but on lines 95-97 (inside the if statement).
Thank you very much for your fast and accurate help. With your suggestions the errors were fixed. Despite, I don't get that script to do what it shall do. Think my approch at point one of your solution was kind of that problem. Tried to reduce the size of the CharacterController Collider, by dividing the size through two.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
[C#]CharacterController Turning 2 Answers
Networked player movement very choppy, rotation works fine - UNET 1 Answer
How to hold objects in third person? 1 Answer
Change Direction[C#] 1 Answer