简体   繁体   English

使我的GameObject正确前进和跳跃的问题

[英]Issues Getting my GameObject to both Move Forward and Jump properly

I'm having an issue where my gameObject barely jumps at all. 我遇到了一个问题,即我的gameObject几乎没有跳跃。 I think it has something to do with moveDirection because the jumping works when I comment out p.velocity = moveDirection . 我认为这与moveDirection因为当我注释掉p.velocity = moveDirection时,跳转有效。

Any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗?

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour 
{    
    public float jumpHeight = 8f;
    public Rigidbody p;    
    public float speed = 1;
    public float runSpeed = 3;
    public Vector3 moveDirection = Vector3.zero;

    // Use this for initialization
    void Start () 
    {
        p = GetComponent<Rigidbody>();
        p.velocity = Vector3.zero;
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKeyDown (KeyCode.Space)) 
        {
            p.AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
        }

        Move ();   
    }

    void Move ()
    {
        if(Input.GetKey(KeyCode.D))
        {
            transform.Rotate(Vector3.up, Mathf.Clamp(180f * Time.deltaTime, 0f, 360f));
        }

        if(Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.up, -Mathf.Clamp(180f * Time.deltaTime, 0f, 360f));
        }

        moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);

        if(Input.GetKey(KeyCode.LeftShift))
        {
            moveDirection *= runSpeed;
        }
        else
        {
            moveDirection *= speed;
        }

        p.velocity = moveDirection;
    }
}

Try using a much higher value for your jumpheight variable. 尝试为您的jumpheight变量使用更高的值。 I usually go with something in the hundreds. 我通常会带几百个东西。

Because right after doing the AddForce(...) literally within the same frame you override the velocity with moveDirection . 因为在字面上在同一帧内执行AddForce(...)之后,您立即使用moveDirection覆盖了速度。 You should be adding to the current velocity, instead of overriding it entirely as such: 您应该添加当前速度,而不是像这样完全覆盖它:

Vector3 velocity = p.velocity;
p.velocity = velocity + moveDirection;

This is exactly why Unity warns against messing with velocity directly , youd be better off just doing another AddForce(...) for your movement: 这就是Unity警告不要直接搞乱速度的原因 ,最好为运动做另一个AddForce(...)

p.AddForce(moveDirection * Time.deltaTime);

EDIT: 编辑:

i don't like digressing too far from the OP's question, but your new problems are probably because you are doing too much with moveDirection half of what i don't even understand why but it should for the most part look like this: 我不喜欢偏离OP的问题太多,但是您的新问题可能是因为您对moveDirection所做的事情太多了,我什至不明白为什么一半,但大部分情况下看起来应该是这样的:

moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")).normalized;
float _speed = Input.GetKey(KeyCode.LeftShift) ? runspeed : speed;

p.AddForce(moveDirection * _speed * Time.deltaTime);

Okay I figured out how to fix it. 好吧,我想出了解决方法。 Instead of those MoveDirection variables I changed it to 而不是将这些MoveDirection变量更改为

if(Input.GetKey(KeyCode.W)) {
        transform.position += transform.forward * Time.deltaTime * speed;
    }
    if(Input.GetKey(KeyCode.S)) {
        transform.position -= transform.forward * Time.deltaTime * speed;
    }

and now it works just fine. 现在可以正常工作了。

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

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