简体   繁体   English

如何使RigidBody 2D在特定的XYZ Unity 3D上跳跃

[英]How to make RigidBody 2D Jump on specific XYZ Unity 3D

I have been googling for like a week, but i still haven't found a thing. 我已经搜索了大约一个星期,但是我仍然没有发现任何东西。 What i need is that the Character(Cat) has to jump on platform, after x clicks on this platform. 我需要的是,在x单击此平台后, Character(Cat)必须在平台上跳转。 There are no movements, only auto jump. 没有动作,只有自动跳转。

This thing is the last thing that should be done. 这件事是应该做的最后一件事。

You can make the cat jump by adding velocity to its rigidbody. 您可以通过向其刚体增加速度来使猫跳。 As for clicking the platform, add a Box Collider 2D to it. 至于单击平台,请向其中添加Box Collider 2D。 Then add this script to the platform. 然后将此脚本添加到平台。

//after the using statements
public GameObject cat; //set this in the inspector by dragging the cat into the variable slot
int clickedAmount = 5;//times it takes to click before the cat jumps
float jumpForce = 10.0f

OnMouseDown() {
    clickedAmount -= 1
    if (clickedAmount == 0) {
         cat.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f,jumpForce));
    }
}

If the cat doesn't appear to jump, make sure you increase the jumpForce variable. 如果猫似乎没有跳跃,请确保增加jumpForce变量。

I noticed your comment, saying you wanted it like in rpgs where you click a position, then your toon moves there. 我注意到了您的评论,说您想要它,就像在rpgs中单击位置一样,然后您的香椿就移到那里。 If that is similar to what you want, there's a few different ways you can do it. 如果这与您想要的相似,则有几种不同的方法可以实现。 You could do something like 你可以做类似的事情

public GameObject cat;
int clickedAmount = 5;

OnMouseDown() {
   clickedAmount -= 1;
   if (clickedAmount <= 0){
       cat.GetComponent<CatControllerScript>().moveToTarget = transform; //Give the cat script the transform of this platform
   } 
}

Then in the cat script do something like 然后在cat脚本中执行类似的操作

public Transform moveToTarget;
public float moveSpeed;
void Update(){
    if (moveToTarget != null){
        //one way is
       transform.position = Vector3.MoveTowards(transform.position, moveToTarget.position, moveSpeed * Time.deltaTime);
        //Another way is 
        transform.position = Vector3.Slerp(transform.position, moveToTarget.position,  moveSpeed * Time.deltaTime);
    }}

Then one you can check the collisions, and if you collide with the transform, make the moveToTarget = null. 然后,您可以检查碰撞,如果与转换发生冲突,则将moveToTarget = null。 Or you can make a bool that is changed, and then just pass the transform seperately. 或者,您可以更改布尔值,然后分别传递变换。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM