Correctly smooth crouch script?
In all internet, there isn't any crouch script that work correctly without mistakes like this: -On crouch/uncrouch the player fall down of the map. -When uncrouch, player go through the ceiling. -Smooth crouch/uncrouch.
It's too hard do this? For me, yes; because I'm a beginner programmer. But for a advanced programmer this must be a easy task (maybe).
A tutorial teaching it would be famous. Why anybody do this?
I only found two styles of videos: -The tutorial with full errors. -The video that show to you his work (a perfect and great work) but without showing his script or how it has done.
I take the opportunity to ask the following: Somebody know any tutorial series that teach you advanced programming in c#? To do scripts like this.
Thanks.
Answer by Zodiarc · Jun 03, 2016 at 02:09 PM
Look for BurgZergArcade on youtube. He has a huge tutorial series on unity and c#.
But to your actual problem:
I can't think about the falling through the worid without looking at the script but you can prevent the clipping through the ceiling by doing a raycast upwards every time the player tries to stand up. If the raycast hits the ceiling that means there's not enough room for him to stand up and you simply don't execute the stand up step.
This is my actually script. The main problem now is that the player uncrouch and through ceilings. How I would do the raycast that you said?
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class Crouch_func : $$anonymous$$onoBehaviour {
// PUBLIC VARS //
public Transform t_mesh; // Player Transform
public CharacterController ccr_controller; // Get the character controller
public bool IsCrouch = false; //
public float LocalScaleY; // Y scale of "t_mesh"
public float ControllerHeight; // Y scale of the character controller
// PRIVATE VARS //
void Start()
{
//nothing here yet
}
void Update()
{
if(Input.GetButtonDown("Crouch"))
{
IsCrouch = !IsCrouch;
CrouchFunction();
}
}
void CrouchFunction()
{
if(IsCrouch == true)
{
t_mesh.localScale = new Vector3(1, LocalScaleY, 1);
ccr_controller.height = ControllerHeight;
Debug.Log("c_func 1");
}
else
{
t_mesh.localScale = new Vector3(1, 1, 1);
ccr_controller.height = 1.8f;
Debug.Log("c_func 0");
}
}
}
Should look something like this:
void CrouchFunction()
{
if(IsCrouch == true)
{
t_mesh.localScale = new Vector3(1, LocalScaleY, 1);
ccr_controller.height = ControllerHeight;
Debug.Log("c_func 1");
}
else
{
Ray ray;
RaycastHit hit;
ray.origin = transform.position;
ray.direction = transform.position.up;
if(!Physics.Raycast(ray, out hit, 1)){
t_mesh.localScale = new Vector3(1, 1, 1);
ccr_controller.height = 1.8f;
Debug.Log("c_func 0");
} else {
Debug.Log("Not enough space to stand up!);
}
}
}
I get an error with this line: ray.direction = transform.position.up;
It underline in red the transform.position.up Upon returning to the editor I have two errors: I've searched info about this error but nothing help me. I must to create a static var? Thanks a lot.
Answer by NateLaw1988 · Aug 19, 2019 at 01:43 PM
Hi I know this is an old post but incase someone new comes along like I did I have posted a video with a project download link to a heavily extended first person controller
Your answer
Follow this Question
Related Questions
SmoothDamp bug? 0 Answers
When I run this code Unity crashes(ANIMATOR RELATED) 0 Answers
What am i doing wrong with my jump attack ? 0 Answers
How to use IF statement ONLY when a certain gameobject is in the scene? 1 Answer
money system in c# 0 Answers