Quantcast
Channel: Questions in topic: "issue"
Viewing all articles
Browse latest Browse all 827

Smooth Crouch Movement Issue

$
0
0
Hello everyone,

I made my character crouch in the game, but I am having this one annoying issue that I don't know how to solve. Whenever the player stands up from the crouch position it has this problem where the player is trying to fall through the ground but then because of the 'StickToGroundForce' it goes right back on the ground again. I'm using the standard Unity 'FirstPersonController'. It looks really buggy and I can't seem to solve it for some reason. Another thing that I found weird is the fact that originally the script was written in Javascript and it works without any problems, but for some reason when I rewritten the script to C# this problem appeared. Even though the script is pretty much the same thing..

If any one could give me some advice on this weird problem I would really appreciate it.

**C# Script:**

using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.FirstPerson; public class Crouch : MonoBehaviour { private CharacterController charController; private float charHeight; //Initial height private GameObject player; private Vector3 theTransform; public bool inVent; public bool inProne; public bool inCrouch; private FirstPersonController controller; void Start () { theTransform = transform.position; player = this.gameObject; charController = GetComponent(); charHeight = charController.height; controller = player.GetComponent(); } void FixedUpdate () { CrouchFunction(); ProneFunction(); } void CrouchFunction () { float h = charHeight; float lastHeight = charController.height; if (Input.GetButton("Crouch") && charController.isGrounded && !controller.m_Jump && !controller.m_onLadder && charController.isGrounded && !inProne) { h = charHeight * 0.4f; charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.y += (charController.height-lastHeight)/2; inCrouch = true; } if (Input.GetButton("Crouch") == false && charController.isGrounded && !inProne) { if(!inVent) { charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.y += (charController.height-lastHeight) / 2; inCrouch = false; } else if(inVent) { h = charHeight * 0.4f; charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.y += (charController.height-lastHeight)/2; inCrouch = true; } } if(controller.m_onLadder || !charController.isGrounded) { charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.y += (charController.height-lastHeight)/2; //Fix vertical position inCrouch = false; } } void ProneFunction () { float h = charHeight; float lastHeight = charController.height; if (Input.GetButtonDown("Prone") && !inProne) { inProne = true; } else if (Input.GetButtonDown("Prone") && inProne) { inProne = false; } if(inProne && charController.isGrounded) { h = charHeight * 0.2f; charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.y += (charController.height - lastHeight) / 2; } } }

**Javascript Script:**

#pragma strict private var charController : CharacterController; private var theTransform : Transform; private var charHeight : float; //Initial height private var player : GameObject; private var defaultWalkSpeed : float; private var defaultRunSpeed : float; public var inVent : boolean; public var inProne : boolean; public var inCrouch : boolean; public var inWater : boolean; private var controller : UnityStandardAssets.Characters.FirstPerson.FirstPersonController; function Start () { theTransform = transform; player = this.gameObject; charController = GetComponent(CharacterController); charHeight = charController.height; controller = player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController); defaultWalkSpeed = player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController).m_WalkSpeed; defaultRunSpeed = player.GetComponent(UnityStandardAssets.Characters.FirstPerson.FirstPersonController).m_RunSpeed; } function Update () { CrouchFunction(); ProneFunction(); } function CrouchFunction () { var h = charHeight; var lastHeight = charController.height; if (Input.GetButton("Crouch") && !inWater && charController.isGrounded && !controller.m_Jump && !controller.m_onLadder && charController.isGrounded && !inProne) { h = charHeight*0.4; charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.position.y += (charController.height-lastHeight)/2; inCrouch = true; } if (Input.GetButton("Crouch") == false && !inWater && charController.isGrounded && !inProne) { if(!inVent) { charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.position.y += (charController.height-lastHeight)/2; inCrouch = false; } else if(inVent) { h = charHeight*0.4; charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.position.y += (charController.height-lastHeight)/2; inCrouch = true; } } if(controller.m_onLadder || !charController.isGrounded || inWater || !charController.isGrounded) { charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.position.y += (charController.height-lastHeight)/2; //Fix vertical position inCrouch = false; } } function ProneFunction () { var h = charHeight; var lastHeight = charController.height; if (Input.GetButtonDown("Prone") && !inProne) { inProne = true; } else if (Input.GetButtonDown("Prone") && inProne) { inProne = false; } if(inProne && !inWater && charController.isGrounded) { h = charHeight*0.2; charController.height = Mathf.Lerp(charController.height, h, 10 * Time.deltaTime); theTransform.position.y += (charController.height-lastHeight) / 2; } } function OnTriggerEnter (col : Collider) { if(col.tag == "Water") { inWater = true; } } function OnTriggerExit (col : Collider) { if(col.tag == "Water") { inWater = false; } }

Viewing all articles
Browse latest Browse all 827

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>