无法在Unity3D上跳转

时间:2015-05-03 23:21:54

标签: c# unity3d

我有关于跳转角色的问题.. 我可以使用CharacterMotor.Move()进行跳转,但没有.SimpleMove(),为什么?

这是我班上的课程:

public float speed = 10.0f;
public float rotationSpeed = 10.0f;
public float jumpSpeed = 50.0f;
public float gravitySpeed = 30.0f;

public string Avancer = "z";
public string Reculer = "s";
public string RotationGauche = "q";
public string RotationDroite = "d";
public string SlideGauche = "a";
public string SlideDroite = "e";
public string Jump = "space";

CharacterController cc;
Vector3 newPos;

public void Start()
{
    cc = GetComponent<CharacterController> ();
}

public void Update()
{
            /*Movements part*/
            if (Input.GetKey (Avancer.ToString ())) {
                    newPos = new Vector3 (speed, 0, 0);
                    newPos = transform.rotation * newPos;
                    cc.SimpleMove (newPos);
            } else if (Input.GetKey (Reculer.ToString ())) {
                    newPos = new Vector3 (speed * -1, 0, 0);
                    newPos = transform.rotation * newPos;
                    cc.SimpleMove (newPos);
            } else if (Input.GetKey (SlideGauche.ToString ())) {
                    newPos = new Vector3 (0, 0, speed);
                    newPos = transform.rotation * newPos;
                    cc.SimpleMove (newPos);
            } else if (Input.GetKey (SlideDroite.ToString ())) {
                    newPos = new Vector3 (0, 0, speed * -1);
                    newPos = transform.rotation * newPos;
                    cc.SimpleMove (newPos);
            } else if (Input.GetKey (RotationGauche.ToString ())) {
                    transform.Rotate (0, rotationSpeed * -1, 0);
            } else if (Input.GetKey (RotationDroite.ToString ())) {
                    transform.Rotate (0, rotationSpeed, 0);
            } else if (cc.isGrounded && Input.GetKeyDown (Jump.ToString ())) {
                    newPos = new Vector3 (0, jumpSpeed * Time.deltaTime, 0);
                    cc.SimpleMove (newPos);
                    //cc.Move (newPos);
                    print ("JUMP !");
            } else if (!cc.isGrounded) {
                    newPos = new Vector3 (0, (gravitySpeed * Time.deltaTime) * -1, 0);
                    cc.SimpleMove (newPos);
                    print ("en l'air");
            }
            /*Movements part*/


    }

我只是不明白它为什么不执行

所以,如果你能帮助我,谢谢你

修改

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class palskie : MonoBehaviour
{   
public float speed = 1000000.0f;
public float rotationSpeed = 10.0f;
public float jumpSpeed = 100.0f;
public float gravitySpeed = 50f;

public string Avancer = "z";
public string Reculer = "s";
public string RotationGauche = "q";
public string RotationDroite = "d";
public string SlideGauche = "a";
public string SlideDroite = "e";
public string Jump = "space";

private CharacterController cc;
Vector3 dir = Vector3.zero;

public int ammo = 100;
public int life = 100;

public void Start()
{
    cc = GetComponent<CharacterController> ();
}

public void Update()
{
            /*Movements part*/
            Vector3 direction;// = Vector3.zero;

            if (cc.isGrounded && Input.GetKeyDown (Jump.ToString ())) {
                    dir.y = jumpSpeed;
                    direction = dir;
            } else if (!cc.isGrounded) {
                    dir.y -= gravitySpeed;
                    direction = dir;
            } else {
                    direction = Vector3.zero;
            }

            if (Input.GetKey (Avancer.ToString ())) {
                    direction.x = speed;


            } else if (Input.GetKey (Reculer.ToString ())) {
                    direction.x -= speed;
            }
            if (Input.GetKey (SlideGauche.ToString ())) {

                    direction.z = speed;
            } else if (Input.GetKey (SlideDroite.ToString ())) {
                    direction.z = -speed;
            }
            if (Input.GetKey (RotationGauche.ToString ())) {
                    transform.Rotate (0, rotationSpeed * -1, 0);
            } else if (Input.GetKey (RotationDroite.ToString ())) {
                    transform.Rotate (0, rotationSpeed, 0);
            }

            //Transformer le Vector3 en direction local
            direction = transform.TransformDirection (direction);

            //Deplacement du personnage
            cc.Move (direction * Time.deltaTime);

            transform.Rotate (0, Input.GetAxis ("Mouse X") * rotationSpeed, 0);
            /*Movements part*/
    }

    void OnTriggerEnter(Collider obj)
    {
            if ((obj.gameObject.name == "Healt")) {
                    life += 5;
                    if (life > 100) 
                            life = 100;
                    Destroy (obj.gameObject);
            } else if ((obj.gameObject.name == "Ammo")) {
                    ammo += 5;
                    if (ammo > 200)
                            ammo = 200;
                    Destroy (obj.gameObject);
            }

    }

}

最后编辑,它的作品! :)经过长时间的搜索我们找到了解决方案 谢谢你的帮助:)

[解决]

1 个答案:

答案 0 :(得分:4)

根据Unity,您不能尝试使用SimpleMove功能跳转。它明确指出documentation SimpleMove页面 SimpleMove 会忽略任何向 y轴添加力的尝试这就是你的意思这样做。它的设计就是出于自己的原因。使用移动功能,因为它们可以跳跃。

如果您使用 SimpleMove 函数,则不需要if语句代码!cc.isGrounded的最后一行。该文件再次声明,当物体移动时,重力会自动应用于它。

出于某种原因,我能够让 SimpleMove 工作,但是将它拉下来的重力太弱了。如果您想尝试此操作,请替换

newPos = new Vector3 (0, jumpSpeed * Time.deltaTime, 0);

newPos = new Vector3 (0, 1 * jumpSpeed * Time.deltaTime, 0);

然后更改public float jumpSpeed = 50.0f;

public float jumpSpeed = 60000f;

它有效,但看起来并不好看。您应该使用Move功能。

修改

&#34;你说SimpleMove()不能在Y轴上做任何事情,那么为什么我的Y轴在我跌倒时编辑,我的意思是重度?&#34 ; 看起来你在怀疑我。没关系。您有责任点击并阅读我发布的链接。

&#34;问题是,如果我不添加重力代码,我的角色会保持飞行,另一个问题是,当我使用Move()跳过时,它会&#39 ;就像传送一样&#34; 这就是你所说的问题是在初学阶段。通过使用 Time.deltaTime 来平滑y轴移动,可以使用Move功能轻松解决。

忘记我上面发布的代码。它工作但在我重新启动Unity后停止工作。我想这是一个让它首先起作用的错误。

我已将代码更改为使用Move功能顺利跳转。我只更改了代码的跳跃重力部分。只需复制整个代码并将其替换为您在上面发布的内容即可。你不应该有任何问题。确保在编辑器中重置 字符控制器值为默认,然后重置脚本的值在编辑器中,以便它将使用脚本中的默认值。测试后,您可以调整 gravitySpeed jumpSpeed 以满足您的需求。

    public float speed = 10.0f;
    public float rotationSpeed = 10.0f;
    public float jumpSpeed = 130;
    public float gravitySpeed = 80.0f;

    public string Avancer = "z";
    public string Reculer = "s";
    public string RotationGauche = "q";
    public string RotationDroite = "d";
    public string SlideGauche = "a";
    public string SlideDroite = "e";
    public string Jump = "space";

    CharacterController cc;
    Vector3 newPos;

    public void Start ()
    {
        cc = GetComponent<CharacterController> ();
    }

    public void Update ()
    {
        /*Movements part*/
        if (Input.GetKey (Avancer.ToString ())) {
            newPos = new Vector3 (speed, 0, 0);
            newPos = transform.rotation * newPos;
            cc.SimpleMove (newPos);
        } else if (Input.GetKey (Reculer.ToString ())) {
            newPos = new Vector3 (speed * -1, 0, 0);
            newPos = transform.rotation * newPos;
            cc.SimpleMove (newPos);
        } else if (Input.GetKey (SlideGauche.ToString ())) {
            newPos = new Vector3 (0, 0, speed);
            newPos = transform.rotation * newPos;
            cc.SimpleMove (newPos);
        } else if (Input.GetKey (SlideDroite.ToString ())) {
            newPos = new Vector3 (0, 0, speed * -1);
            newPos = transform.rotation * newPos;
            cc.SimpleMove (newPos);
        } else if (Input.GetKey (RotationGauche.ToString ())) {
            transform.Rotate (0, rotationSpeed * -1, 0);
        } else if (Input.GetKey (RotationDroite.ToString ())) {
            transform.Rotate (0, rotationSpeed, 0);
        } else if (cc.isGrounded && Input.GetKeyDown (Jump.ToString ())) {
            newPos = new Vector3 (0, jumpSpeed, 0);
            cc.Move (newPos * Time.deltaTime);
            print ("JUMP !");
        } 

        if (!cc.isGrounded) {
            newPos.y -= gravitySpeed * Time.deltaTime;
            cc.Move (newPos * Time.deltaTime);
            print ("en l'air");
        }
        /*Movements part*/


    }