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.