我遇到的问题是我的gameObject几乎没有跳过。我认为这与moveDirection
有关,因为当我评论p.velocity = moveDirection
时,跳跃有效。
有关如何解决此问题的任何建议?
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;
}
}
答案 0 :(得分:0)
尝试为jumpheight变量使用更高的值。我通常会选择数以百计的东西。
答案 1 :(得分:0)
因为在myDiv.innerHTML = "<img src='http://127.0.0.2:8080/prime_almost_full_00007.0001.png' width='256' height='256' class='reel' id='image' data-images='http://127.0.0.2:8080/prime_almost_full_00007.####.png|0001..0021'>";
$.reel.scan();
字面意义上在同一帧内,你用AddForce(...)
覆盖了速度。你应该添加到当前的速度,而不是完全覆盖它:
moveDirection
这正是Unity warns against messing with velocity directly的原因,你最好再为你的运动做另一个Vector3 velocity = p.velocity;
p.velocity = velocity + moveDirection;
:
AddForce(...)
编辑:
我不喜欢离开OP的问题太远了,但是你的新问题可能是因为你做了太多p.AddForce(moveDirection * Time.deltaTime);
一半我甚至不做明白为什么,但它应该在大多数情况下看起来像这样:
moveDirection
答案 2 :(得分:0)
好的,我想出了如何修复它。而不是那些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;
}
现在它工作得很好。