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 - Fatal编程技术网

C# 跳跃时更改角色方向

C# 跳跃时更改角色方向,c#,unity3d,C#,Unity3d,我正在制作一个2D平台,我按照一个教程来构建我的角色。除了跳跃时不允许在空中改变方向外,该角色工作正常。我如何才能添加到这一点,使我的角色能够改变方向中跳 用于基本运动的代码如下所示: void Update() { CharacterController controller = GetComponent<CharacterController>(); float rotation = Input.GetAxis("Horizontal"); if(con

我正在制作一个2D平台,我按照一个教程来构建我的角色。除了跳跃时不允许在空中改变方向外,该角色工作正常。我如何才能添加到这一点,使我的角色能够改变方向中跳

用于基本运动的代码如下所示:

void Update()
{

    CharacterController controller = GetComponent<CharacterController>();
    float rotation = Input.GetAxis("Horizontal");
    if(controller.isGrounded)
    {
        moveDirection.Set(rotation, 0, 0);
        moveDirection = transform.TransformDirection(moveDirection);

        //running code
        if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
        { running = true; }
        else
        { running = false; }

        moveDirection *= running ? runningSpeed : walkingSpeed; //set speed

        //jump code
        if(Input.GetButtonDown("Jump"))
        {
            jump();
        }
    }

    moveDirection.y -= gravity * Time.deltaTime;

    controller.Move(moveDirection * Time.deltaTime);


}

仅当角色固定时,才更新
moveDirection
向量:

if(controller.isGrounded)
当玩家跳跃时,
isGrounded
设置为false,因此
moveDirection
不会被更新(重力除外)

我想你不想影响角色跳跃时的速度(除非它应该在空中飞行或行走)。我猜您希望在它跳跃时更改其方向,以便可以直接修改变换,使其根据您的输入旋转

比如:

else
{
   float amountOfRotation = Input.GetAxis("...") * rotationScaleFactor; 
   transform.RotateAround(transform.position, transform.up, Time.deltaTime * amountOfRotation);
}

编辑

我从未使用过它,但我认为CharacterController是为仅在对象固定时移动而设计的。因此,如果您想在播放时移动它,请不要使用移动方法,而是直接编辑游戏对象的变换:

else
{
   float additionalForwardSpeed = Input.GetAxis("...") * speedScaleFactor;  
   transform.Translate(transform.forward * Time.deltaTime * additionalForwardSpeed ); //translate
}

上面的代码应该可以提高对象在局部前进方向上的速度。

跳跃()中的代码是什么!编辑了问题。只是一个猜测;从
if
块中排除
moveDirection
的定义。在它之前执行它。所以:
moveDirection.Set。。。;移动方向=。。。;如果(controller.isground)
。奇怪的是,这会阻止角色跳得很高。它只是刚离开地面,然后又被推回地面。出于好奇,为什么要取消变换的父级?我实际上根本不想让角色旋转。我不一定希望它在空中行走,但我希望它在空中能稍微改变方向,以防意外跳跃。想象一个马里奥游戏(2D platformer),他能够在空中轻微移动。@rar:ok..而不是在else语句中简单地移动它而不是旋转它。添加
else{moveDirection.Set(rotation,0,0);moveDirection=transform.TransformDirection(moveDirection);}
只需让它在跳跃后立即返回地面即可。CharacterController不是为在空中移动而设计的,您是对的。但是你提供的平移使我在z方向移动。我不知道如何改变前进方向,但为什么我总是希望它向前?我写的跳跃允许我向某个方向跳跃,我根本无法在空中改变它。@rar:我的只是一个例子。试着理解这个想法,然后根据你的需要调整它。
else
{
   float additionalForwardSpeed = Input.GetAxis("...") * speedScaleFactor;  
   transform.Translate(transform.forward * Time.deltaTime * additionalForwardSpeed ); //translate
}