我在地形上工作,做了这个坑,我的角色控制器的行为就像
它在地面上,但肯定是漂浮的,当我向前移动时,它将被重力拖下去,但它又再次“卡住”了漂浮,如果我在深坑的底部并尝试爬升,它实际上会爬升大坡度,而我的坡度限制是45,它的计数类似于它的接地,因此我可以再次跳跃,是的,我尝试进行了射线广播以检查其是否接地,但是仍然只能解决攀爬的问题,但是如果我跳到侧面,仍然可以坑我会开始漂浮。
这是脚本。
using UnityEngine;
using System.Collections;
public class moveController : MonoBehaviour
{
private CharacterController controller;
public float moveSpeed = 15f;
public float jumpForce = 15f;
private Vector3 moveDirection;
private float gravityScale = 0.1f;
private float up_Down;
private float right_Left;
private float mouseX;
private float mouseY;
//headbob
private float currentAngle = 0;
private float smooth = 20.0F;
private float tiltAngle = 1.0F;
private float tiltAroundZ;
Vector3 h_bob;
//AccelerationVariables
private float jumpVelocityPerSecond = 3f;
private float speedVelocityPerSecond = 3f;
// jump or not , moving or not , walking or not,
private bool isJumping;
private bool isMoving;
private bool isWalking;
// look variables
Vector2 mouselook;
Vector2 smoothV;
private float smoothing = 2f;
private float sensitivity = 1.5f;
private Camera eyes;
private void Awake()
{
controller = GetComponent<CharacterController>();
eyes = Camera.main;
}
void Start()
{
}
void Update()
{
Move();
SlowDown();
//DeadHop();
// MaxSpeed_Jump();
Crouch();
}
private void Move()
{
// moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
up_Down = Input.GetAxis("Vertical");
right_Left = Input.GetAxis("Horizontal");
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
currentAngle = Mathf.MoveTowards(currentAngle, tiltAroundZ, Time.deltaTime * smooth);
Vector3 moveDirSide = transform.right * right_Left * moveSpeed;
Vector3 moveDirForward = transform.forward * up_Down * moveSpeed ;
Vector3 BodyRot = transform.rotation.eulerAngles;
BodyRot.y += mouseX;
var md = new Vector2(mouseX, mouseY);
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouselook += smoothV;
mouselook.y = Mathf.Clamp(mouselook.y, -80f, 80f);
// check if he is moving
if (Input.GetButton("Horizontal"))
{
isMoving = true;
}
else if (Input.GetButton("Vertical"))
{
isMoving = true;
}
else
{
isMoving = false;
}
// check if he is jumping
if (Input.GetButton("Jump"))
{
isJumping = true;
}
else if (Input.GetButtonUp("Jump"))
{
isJumping = false;
}
///check if grounded so he can jump
if (controller.isGrounded)
{
if (isJumping)
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
controller.Move(moveDirSide * Time.deltaTime);
controller.Move(moveDirForward * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
eyes.transform.localRotation = Quaternion.Euler(-mouselook.y, Vector3.right.x, -currentAngle);
transform.localRotation = Quaternion.AngleAxis(mouselook.x, BodyRot);
}
a
private void SlowDown()
{
if (Input.GetButton("SlowDown"))
{
moveSpeed = 5f;
jumpForce = 10f;
isWalking = true;
}
else if (Input.GetButtonUp("SlowDown"))
{
moveSpeed = 15f;
jumpForce = 15f;
isWalking = false;
}
}
//private void DeadHop()
//{
// if (isMoving)
// {
// jumpForce += jumpVelocityPerSecond * Time.deltaTime ;
// moveSpeed += speedVelocityPerSecond * Time.deltaTime ;
// }
// else if (isMoving == false)
// {
// jumpForce = 10f;
// moveSpeed = 10f;
// }
//}
//private void MaxSpeed_Jump()
//{
// if(moveSpeed >= 15)
// {
// moveSpeed = 15;
// }
// if(jumpForce >= 15)
// {
// jumpForce = 15;
// }
//}
private void Crouch()
{
if (Input.GetButton("Crouch") && isJumping == false)
{
controller.height = 1.5f;
moveSpeed = 5f;
}
else if (Input.GetButtonUp("Crouch") && isJumping == false)
{
moveSpeed = 15f;
controller.height = 3f;
}
if (Input.GetButton("Crouch") && isJumping == true)
{
controller.height = 1.5f;
}
else if (Input.GetButtonUp("Crouch") && isJumping == true)
{
moveSpeed = 15f;
controller.height = 3f;
}
}
}