Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
统一中的平稳下降-C#_C#_Unity3d_3d_Physics_Smooth - Fatal编程技术网

统一中的平稳下降-C#

统一中的平稳下降-C#,c#,unity3d,3d,physics,smooth,C#,Unity3d,3d,Physics,Smooth,我正在编写一个角色控制器脚本,一切正常,但问题是,一旦我的玩家开始摔倒,它就会突然倒下。我希望玩家逐渐倒下,这是我的角色控制器脚本- using UnityEngine; using System.Collections; public class CharacterController : MonoBehaviour { public float inputDelay = 0.1f; public float forwardVel = 12; public floa

我正在编写一个角色控制器脚本,一切正常,但问题是,一旦我的玩家开始摔倒,它就会突然倒下。我希望玩家逐渐倒下,这是我的角色控制器脚本-

using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour {

    public float inputDelay = 0.1f;
    public float forwardVel = 12;
    public float rotateCel = 12;
    public float JumpHeight = 20;
    public Vector3 Gravity = new Vector3 (0, -180, 0);
    public bool CanPress;
    private float jumpTime;
    public float _initialJumpTime = 0.4f;
    //[HideInInspector]
    public bool isGrounded;
    Quaternion targetRotation;
    Rigidbody rBody;
    Vector3 forwardInput, turnInput;
    public bool HasJumped;

    public Quaternion TargetRotation
    {
        get {return targetRotation;}
    }
    // Use this for initialization
    void Start () {
        Physics.gravity = Gravity;
        targetRotation = transform.rotation;
        if (GetComponent<Rigidbody> ())
            rBody = GetComponent<Rigidbody> ();
        else {
            Debug.LogError("Character Needs Rigidbody");
        }

    //  forwardInput = turnInput = 0;
        forwardInput = turnInput = Vector3.zero;
    }

    // Update is called once per frame
    void Update () {

        GetInput ();
        //Turn ();

        if (CanPress == true) {
            if (Input.GetKeyDown (KeyCode.Space)) { 
                HasJumped = true;
                jumpTime = _initialJumpTime;
            }
        }

        if (HasJumped == true) {
            rBody.useGravity = false;
            jumpTime -= 1 * Time.deltaTime;
            if (jumpTime > 0) {
            Jump();
            }
            else {
                HasJumped = false;
                rBody.useGravity = true;
            }
        }
    }


    void GetInput() {
        //forwardInput = Input.GetAxis ("Vertical");
        //turnInput = Input.GetAxis ("Horizontal");
        forwardInput = new Vector3 (Input.GetAxis ("Horizontal") * rotateCel, 0, Input.GetAxis ("Vertical") * forwardVel);
        forwardInput = transform.TransformDirection (forwardInput);
        if (Input.GetKeyUp (KeyCode.Space)) {
            //HasJumped = false;
        }


    }

    void Jump() { 

            Vector3 up = transform.TransformDirection (Vector3.up);
        GetComponent<Rigidbody> ().AddForce (up * 5, ForceMode.Impulse);

    }
    void FixedUpdate() {

        Run ();
    }
    void Run() {
        if (Mathf.Abs (10) > inputDelay) {
            //Move
            //rBody.velocity = transform.forward * forwardInput * forwardVel;
            rBody.velocity = forwardInput;
        } else {
            //zero velocity
            rBody.velocity = Vector3.zero;

        }

    }
    void Turn() {
    //  targetRotation *= Quaternion.AngleAxis (rotateCel * turnInput * Time.deltaTime, Vector3.up);
    //  transform.rotation = targetRotation;
    }

    void OnTriggerEnter(Collider col) {
        isGrounded = true;
        CanPress = true;
    }

    void OnTriggerExit(Collider col) {
        isGrounded = false;
        CanPress = false;
    }

}
使用UnityEngine;
使用系统集合;
公共类CharacterController:单行为{
公共浮点输入延迟=0.1f;
公共浮动前级=12;
公共浮点数=12;
公共浮子高度=20;
公共向量3重力=新向量3(0,-180,0);
公共图书馆;
私人浮动跳转时间;
公共浮点数_initialJumpTime=0.4f;
//[hideininstecpt]
公共学校停课;
四元数定向;
刚体;
矢量3正向输入,反向输入;
公众的注意力已经跳跃;
公共四元数目标
{
获取{return targetRotation;}
}
//用于初始化
无效开始(){
物理学。重力=重力;
targetRotation=变换旋转;
if(GetComponent())
rBody=GetComponent();
否则{
LogError(“角色需要刚体”);
}
//forwardInput=turnInput=0;
forwardInput=turnInput=Vector3.0;
}
//每帧调用一次更新
无效更新(){
GetInput();
//转身();
如果(CanPress==true){
if(Input.GetKeyDown(KeyCode.Space)){
hasshopped=true;
jumpTime=\u初始jumpTime;
}
}
if(hasshopped==true){
rBody.useGravity=false;
跳跃时间-=1*Time.deltaTime;
如果(跳转时间>0){
跳跃();
}
否则{
hasshopped=false;
rBody.useGravity=true;
}
}
}
void GetInput(){
//forwardInput=Input.GetAxis(“垂直”);
//turnInput=Input.GetAxis(“水平”);
forwardInput=新的矢量3(Input.GetAxis(“水平”)*rotateCel,0,Input.GetAxis(“垂直”)*ForwardLevel);
forwardInput=transform.TransformDirection(forwardInput);
if(Input.GetKeyUp(KeyCode.Space)){
//hasshopped=false;
}
}
无效跳转(){
Vector3 up=transform.TransformDirection(Vector3.up);
GetComponent().AddForce(up*5,ForceMode.pulse);
}
void FixedUpdate(){
运行();
}
无效运行(){
if(Mathf.Abs(10)>输入延迟){
//移动
//rBody.velocity=transform.forward*forwardInput*forwardVel;
rBody.velocity=正向输入;
}否则{
//零速度
rBody.velocity=Vector3.0;
}
}
无效回合(){
//targetRotation*=四元数.AngleAxis(rotateCel*turnInput*Time.deltaTime,Vector3.up);
//transform.rotation=目标定位;
}
无效对撞机(对撞机列){
isGrounded=true;
CanPress=true;
}
void OnTriggerExit(碰撞器列){
isfounded=false;
CanPress=false;
}
}
我的角色有一个刚体附件,它使用重力并有X、Y、Z旋转约束

  • 球员上下平稳,但两者之间的过渡非常突然

谢谢你的帮助。:)

好吧,我拿了你的密码玩了一场

我认为问题在于,在你的
Run()
函数中,你改变了
刚体的速度,这影响了你的跳跃

我已经删除了这些内容,稍微改进了你的脚本,并对其进行了测试。将其连接到一个装有
刚体
的胶囊上,底部有一个装有
盒式对撞机
的地板,然后击中太空,你应该能平稳地跳跃

您的新(ish)脚本:

“如何调整玩家的跳跃速度”

您可以更改
jumpHeight
变量以获得更高或更小的跳跃。如果您所说的“更快”是指下落更快,请转到“编辑>项目设置>物理>并将Y重力更改为更小的值,例如-20

可以找到教程


他们有各种各样的教程,甚至还有一些与示例项目一起提供的教程,因此您可以在视频之后将其组合在一起。

您是否正在构建自己的角色控制器?如果你是,我很好奇为什么你不使用内置字符控制器作为Unity标准资产的一部分,这为你做了一切:-)嘿@Tom,我做所有这些都是作为学习经验的一部分,因为我不想一直使用Unity的内置脚本,创建我自己的脚本让我可以选择更改脚本以满足我的游戏需求。哇,谢谢@Tom,我会记住这个建议:)我会测试代码并告诉你它是否有效。:)嘿,汤姆,现在跳得很好。有几个问题-你建议我用刚体来移动玩家,还是只用Vector3.Lerp?我该如何调整玩家跳跃的速度,因为我只有13岁,在编码方面没有太多经验,有没有网站或教程详细介绍C#编码?非常感谢您的时间和帮助。很高兴为您编码!:谢谢你,这回答了我所有的问题。从Objective-C到C#的巨大变化:我不知道你是否听说过Ray Wenderlich网站,但这是我的教程,谢谢你的帮助D
using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour
{
    public float inputDelay = 0.1f;
    public float forwardVel = 12;
    public float rotateCel = 12;
    public float jumpHeight = 10;
    private float jumpTime;
    public float _initialJumpTime = 0.4f;
    //[HideInInspector]
    public bool isGrounded;
    Quaternion targetRotation;
    Rigidbody rBody;
    Vector3 forwardInput, turnInput;
    public bool canJump;

    public Quaternion TargetRotation
    {
        get { return targetRotation; }
    }

    void Start()
    {
        targetRotation = transform.rotation;
        if (GetComponent<Rigidbody>())
            rBody = GetComponent<Rigidbody>();
        else
        {
            Debug.LogError("Character Needs Rigidbody");
        }

        //  forwardInput = turnInput = 0;
        forwardInput = turnInput = Vector3.zero;
    }

    void Update()
    {
        GetInput();
        //Turn ();

        if (Input.GetKeyDown(KeyCode.Space) && canJump)
        {
            rBody.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
        }
    }


    void GetInput()
    {
        //forwardInput = Input.GetAxis ("Vertical");
        //turnInput = Input.GetAxis ("Horizontal");
        forwardInput = new Vector3(Input.GetAxis("Horizontal") * rotateCel, 0, Input.GetAxis("Vertical") * forwardVel);
        forwardInput = transform.TransformDirection(forwardInput);
    }

    void FixedUpdate()
    {
        //Run();
    }

    void Run()
    {
        //HERE YOU SET THE RIGIDBODYS VELOCITY, WHICH IS CAUSING YOUR JUMP TO NOT WORK PROPERLY. DO NOT MODIFY THE VELOCITY
        //OF A RIGIDBODY
        if (Mathf.Abs(10) > inputDelay)
        {
            //Move
            //rBody.velocity = transform.forward * forwardInput * forwardVel;
            rBody.velocity = forwardInput;
        }
        else
        {
            //zero velocity
            rBody.velocity = Vector3.zero;
        }
    }

    void Turn()
    {
        //  targetRotation *= Quaternion.AngleAxis (rotateCel * turnInput * Time.deltaTime, Vector3.up);
        //  transform.rotation = targetRotation;
    }

    void OnCollisionEnter(Collision col)
    {
        isGrounded = true;
        canJump = true;
    }

    void OnCollisionExit(Collision col)
    {
        isGrounded = false;
        canJump = false;
    }
}
void Update()
{
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        transform.position += Vector3.left * speed * Time.deltaTime; //speed could be 5f, for example (f means float);
    }

    // then do the same for other directions, RightArrow -.right, UpArrow - .forward, DownArrow - .back
}