【问题标题】:how can i change the x position while jumping on air using c#?如何在使用 c# 进行空中跳跃时更改 x 位置?
【发布时间】:2012-03-22 16:03:59
【问题描述】:

我有一个可以跳跃的角色控制器,但是在跳跃时我想改变角色的 x 位置,所以基本上他可以在跳跃时转身,这是我目前的尝试

     //start of character controller
    CharacterController controller = GetComponent<CharacterController>();
    if (controller.isGrounded) {

     //get the player vector movement vector
         movePlayer = new Vector3(Input.acceleration.x,0,1);
         //float h = Input.acceleration.x;
         //translate the players movement
         transform.Translate(movePlayer * moveSpeed * Time.deltaTime);
         //the run animation
         animation.CrossFade("run");

    //restrict the movement of x-axis for player
    Vector3 pos = transform.position;
    pos.x = Mathf.Clamp(pos.x, -3.0f, 3.0f);
    transform.position = pos;

   if (Input.GetButton("Jump")){
    //my ptoblem is here, the x axis on the vector3 is not happening    
    movePlayer =transform.TransformDirection(new   Vector3(Input.acceleration.x,jumpSpeed,forwardJumpSpeed)); 


              }  

         }

    // attach the gravity and move controller     
    movePlayer.y -= gravity * Time.deltaTime;
    controller.Move(movePlayer * Time.deltaTime);

当前代码:

void Update() {

    CharacterController controller = GetComponent<CharacterController>();

         //get the player vector movement vector
         movePlayer = new Vector3(Input.GetAxis("Horizontal"),0,1);
         //float h = Input.acceleration.x;
         //translate the players movement
         transform.Translate(movePlayer * moveSpeed * Time.deltaTime);
         //the run animation
         animation.CrossFade("run");

    //restrict the movement of x-axis for player
    Vector3 pos = transform.position;
    pos.x = Mathf.Clamp(pos.x, -3.0f, 3.0f);
    transform.position = pos;

    if (controller.isGrounded) {

   if (Input.GetButton("Jump")){

    movePlayer =transform.TransformDirection(new Vector3(Input.acceleration.x,jumpSpeed,forwardJumpSpeed)); 


              }


         }

     movePlayer.y -= gravity * Time.deltaTime;
    controller.Move(movePlayer * Time.deltaTime);

这是我当前的代码,跳转 if 语句在 if(controller.isGrounded) 内,字符仍然移动,但是当我按下空格键时它不再跳转。

【问题讨论】:

  • 顺便说一下我的问题出在哪里,而且所有代码都可以正常工作!
  • 为什么会有Controller.Move()transfrom.Translate()
  • 是的,我知道我认为这是问题所在,抱歉,我删除了 transform.translate 并保留了 controller.move(),但它仍然没有跳转我尝试放置打印语句(“我们已接地"),在 if(controller.isGrounded) 之后,它没有打印,所以基本上统一没有为我们检测到地面,奇怪
  • 代码看起来不错,但您的场景中有问题。另外,您是否指定了重力值?

标签: c# unity3d


【解决方案1】:

当玩家跳跃时,它不再接地,因此if(controller.isGrounded) 下的任何代码都不会被调用。把你的运动代码放在外面,但把跳跃代码放在里面,它会正常工作的。

// movement code goes here

// Keep the jumping code inside this if-statement
if (controller.isGrounded) {
    if (Input.GetButton("Jump")){  
       movePlayer =transform.TransformDirection(new Vector3(Input.acceleration.x,jumpSpeed,forwardJumpSpeed)); 
    }

【讨论】:

  • 按空格键时角色不再跳跃! :)
  • 当我按空格键时,字符不会像以前一样沿 y 轴上升,但是当我在 isGrounded 内移动移动代码时它可以工作:))
  • 当我删除 isGrounded 之外的移动代码时,跳跃的 deosnt 工作,它很奇怪,但我认为它可能是按钮上的 controller.move 命令
  • 我不明白你做了什么。你能用你当前的代码更新问题吗?
猜你喜欢
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 2015-09-11
相关资源
最近更新 更多