C# 爬坡地形统一字符控制器

C# 爬坡地形统一字符控制器,c#,unity3d,game-physics,C#,Unity3d,Game Physics,我在地形上工作,我制作了这个坑,我的角色控制器就像 它在地面上,但它肯定是漂浮的,当我向前移动时,它会被重力拖下来,但它会“卡住”再次漂浮,如果我在坑底尝试攀爬,它实际上会爬上大斜坡,而我的坡度限制是45,它的计数与它的接地一样,所以我可以再次跳跃,是的,我尝试进行光线投射以检查它是否接地,但它仍然只能解决攀爬的问题,但如果我在坑边跳跃,我仍然会开始浮动。 这是剧本 using UnityEngine; using System.Collections; public class moveCo

我在地形上工作,我制作了这个坑,我的角色控制器就像 它在地面上,但它肯定是漂浮的,当我向前移动时,它会被重力拖下来,但它会“卡住”再次漂浮,如果我在坑底尝试攀爬,它实际上会爬上大斜坡,而我的坡度限制是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;
        }


    }   
}
使用UnityEngine;
使用系统集合;
公共类控制器:单行为
{
专用字符控制器;
公共浮子移动速度=15f;
公众浮力=15f;
专用矢量3移动方向;
专用浮球重力标度=0.1f;
私家车上下浮动;
私人浮动右/左;
私人浮动鼠标;
私人浮动鼠标;
//头锤
专用浮点currentAngle=0;
私人浮动平滑=20.0F;
专用浮子倾角=1.0F;
私人浮动tiltAroundZ;
向量3h_-bob;
//加速变量
专用浮点数跳线速度TyperSecond=3f;
专用浮点数速度类型秒=3f;
//跳不跳,动不动,走不走,
私家侦探在跳;
私人住宅;
二等兵布尔正在行走;
//外观变量
Vector2鼠标器;
矢量2平滑V;
私有浮点平滑=2f;
私人浮动灵敏度=1.5f;
私人摄像机眼睛;
私人空间
{
控制器=GetComponent();
眼睛=照相机.main;
}
void Start()
{
}
无效更新()
{
Move();
减速();
//DeadHop();
//最大速度跳跃();
蹲下();
}
私人空位移动()
{
//moveDirection=newvector3(Input.GetAxis(“水平”)*moveSpeed,moveDirection.y,Input.GetAxis(“垂直”)*moveSpeed);
向上向下=Input.GetAxis(“垂直”);
right_Left=Input.GetAxis(“水平”);
mouseX=Input.GetAxis(“鼠标X”);
mouseY=Input.GetAxis(“鼠标Y”);
tiltAroundZ=输入。GetAxis(“水平”)*倾斜角;
currentAngle=数学移动方向(currentAngle、tiltAroundZ、Time.deltaTime*平滑);
Vector3 moveDirSide=transform.right*right\u Left*移动速度;
Vector3 moveDirForward=transform.forward*上下*移动速度;
Vector3 BodyRot=transform.rotation.eulerAngles;
BodyRot.y+=鼠标;
var md=新向量2(mouseX,mouseY);
md=向量2.标度(md,新向量2(灵敏度*平滑,灵敏度*平滑));
smoothV.x=数学Lerp(smoothV.x,md.x,1f/平滑);
smoothV.y=数学Lerp(smoothV.y,md.y,1f/平滑);
mouselook+=smoothV;
mouselook.y=Mathf.Clamp(mouselook.y,-80f,80f);
//检查他是否在移动
if(Input.GetButton(“水平”))
{
isMoving=真;
}
else if(Input.GetButton(“垂直”))
{
isMoving=真;
}
其他的
{
isMoving=假;
}
//检查他是否在跳
if(Input.GetButton(“跳转”))
{
isJumping=true;
}
else if(Input.GetButtonUp(“Jump”))
{
isJumping=false;
}
///检查是否接地,这样他就可以跳了
if(controller.isground)
{
如果(正在跳跃)
{             
移动方向。y=跳跃力;
}
}
moveDirection.y=moveDirection.y+(Physics.gravity.y*gravityScale);
controller.Move(moveDirSide*Time.deltaTime);
controller.Move(moveDirForward*Time.deltaTime);
控制器移动(移动方向*时间增量);
eyes.transform.localRotation=Quaternion.Euler(-mouselook.y,Vector3.right.x,-currentAngle);
transform.localRotation=Quaternion.AngleAxis(mouselook.x,BodyRot);
}
A.
私人股本
{
if(Input.GetButton(“减速”))
{
移动速度=5f;
跳跃力=10f;
isWalking=true;
}
否则if(Input.GetButtonUp(“减速”)
{
移动速度=15f;
跳跃力=15f;
isWalking=false;
}
}
//私有void DeadHop()
//{
//如果(正在移动)
//    {
//jumpForce+=jumpVelocityPerSecond*Time.deltaTime;
//moveSpeed+=speedVelocityPerSecond*Time.deltaTime;
//    }
//else if(isMoving==false)
//    {
//跳跃力=10f;
//移动速度=10f;
//    }
//}
//私有void MaxSpeed_Jump()
//{
//如果(移动速度>=15)
//    {
//移动速度=15;
//    }
//如果(跳线力>=15)
//    {
//跳跃力=15;
//    }
//}
私人空蹲()
{
if(Input.GetButton(“cruch”)和&isJumping==false)
{
控制器高度=1.5f;
移动速度=5f;
}
else if(Input.GetButtonUp(“cruch”)和&isJumping==false)
{
移动速度=15f;
控制器高度=3f;
}
if(Input.GetButton(“cruch”)和&isJumping==true)
{
控制器高度=1.5f;
}
else if(Input.GetButtonUp(“cruch”)和&isJumping==true)
{
移动速度=15f;
控制器高度=3f;
}
}   
}