自 Unity 5 以来,跳跃光线投射 isGrounded 无法正常工作
Jump raycasting isGrounded not working properly since Unity 5
我刚才问了一个问题,关于光线投射让玩家在接触地面时跳跃,我得到了很大的帮助并解决了我的问题。自从将我的项目更新到 Unity 5 后,我的播放器不再跳转并且关于它的 if 语句永远不会 运行:
if(Physics.Raycast(ray, rayDistance, 1 << 8))
我不确定为什么会这样,在回到我上一个回答的问题后,我无法解决这个问题。如果有人能提供帮助,我将不胜感激,因为我对 Unity 和 C# 还比较陌生。谢谢
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Update is called once per frame
void FixedUpdate() {
// Creating floats to hold the speed of the plater
float playerSpeedHorizontal = 4f * Input.GetAxis ("Horizontal");
float playerSpeedVertical = 4f * Input.GetAxis ("Vertical");
//Vector3 velocity = transform.forward * playerSpeedVertical + transform.right * playerSpeedHorizontal;
//velocity.Normalize();
//velocity *= playerMaxSpeed;
//velocity.y = rigidbody.velocity.y;
//rigidbody.velocity = velocity;
// Transform statements to move the player by the playerSpeed amount.
transform.Translate (Vector3.forward * playerSpeedVertical * Time.deltaTime);
transform.Translate (Vector3.right * playerSpeedHorizontal * Time.deltaTime);
// Calling the playerJump function when the jump key is pressed
if (Input.GetButton("Jump"))
{
playerJump();
}
}
/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {
const float JumpForce = 1.75f;
Debug.Log ("Should Jump");
Vector3 rayOrigin = transform.position;
rayOrigin.y += GetComponent<Collider>().bounds.extents.y; //move the ray origin up into the collider so that it won't begin in the ground / floor
float rayDistance = GetComponent<Collider>().bounds.extents.y + 0.1f;
Ray ray = new Ray ();
ray.origin = rayOrigin;
ray.direction = Vector3.down;
if(Physics.Raycast(ray, rayDistance, 1 << 8)) {
Debug.Log ("Jumping");
GetComponent<Rigidbody>().AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
}
以前,您的代码依赖于游戏对象的位置与其边界中心是否相同。有可能当你更新到unity5时这可能已经改变了,但可能还有其他原因。
解决方案是从边界的中心检查,而不是从对象的中心(它的位置)检查。因为您使用 bounds.extents 来获取从边界中心到边界边缘的距离,所以如果边界中心和游戏对象中心不同,那将不再有效。
这是应该执行此操作的更改代码:
/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {
const float JumpForce = 1.75f;
Debug.Log ("Should Jump");
Vector3 rayOrigin = GetComponent<Collider>().bounds.center;
float rayDistance = GetComponent<Collider>().bounds.extents.y + 0.1f;
Ray ray = new Ray ();
ray.origin = rayOrigin;
ray.direction = Vector3.down;
if(Physics.Raycast(ray, rayDistance, 1 << 8)) {
Debug.Log ("Jumping");
GetComponent<Rigidbody>().AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
}
我刚才问了一个问题,关于光线投射让玩家在接触地面时跳跃,我得到了很大的帮助并解决了我的问题。自从将我的项目更新到 Unity 5 后,我的播放器不再跳转并且关于它的 if 语句永远不会 运行:
if(Physics.Raycast(ray, rayDistance, 1 << 8))
我不确定为什么会这样,在回到我上一个回答的问题后,我无法解决这个问题。如果有人能提供帮助,我将不胜感激,因为我对 Unity 和 C# 还比较陌生。谢谢
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Update is called once per frame
void FixedUpdate() {
// Creating floats to hold the speed of the plater
float playerSpeedHorizontal = 4f * Input.GetAxis ("Horizontal");
float playerSpeedVertical = 4f * Input.GetAxis ("Vertical");
//Vector3 velocity = transform.forward * playerSpeedVertical + transform.right * playerSpeedHorizontal;
//velocity.Normalize();
//velocity *= playerMaxSpeed;
//velocity.y = rigidbody.velocity.y;
//rigidbody.velocity = velocity;
// Transform statements to move the player by the playerSpeed amount.
transform.Translate (Vector3.forward * playerSpeedVertical * Time.deltaTime);
transform.Translate (Vector3.right * playerSpeedHorizontal * Time.deltaTime);
// Calling the playerJump function when the jump key is pressed
if (Input.GetButton("Jump"))
{
playerJump();
}
}
/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {
const float JumpForce = 1.75f;
Debug.Log ("Should Jump");
Vector3 rayOrigin = transform.position;
rayOrigin.y += GetComponent<Collider>().bounds.extents.y; //move the ray origin up into the collider so that it won't begin in the ground / floor
float rayDistance = GetComponent<Collider>().bounds.extents.y + 0.1f;
Ray ray = new Ray ();
ray.origin = rayOrigin;
ray.direction = Vector3.down;
if(Physics.Raycast(ray, rayDistance, 1 << 8)) {
Debug.Log ("Jumping");
GetComponent<Rigidbody>().AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
}
以前,您的代码依赖于游戏对象的位置与其边界中心是否相同。有可能当你更新到unity5时这可能已经改变了,但可能还有其他原因。
解决方案是从边界的中心检查,而不是从对象的中心(它的位置)检查。因为您使用 bounds.extents 来获取从边界中心到边界边缘的距离,所以如果边界中心和游戏对象中心不同,那将不再有效。
这是应该执行此操作的更改代码:
/// Here we handle anything to do with the jump, including the raycast, any animations, and the force setting it's self.
void playerJump() {
const float JumpForce = 1.75f;
Debug.Log ("Should Jump");
Vector3 rayOrigin = GetComponent<Collider>().bounds.center;
float rayDistance = GetComponent<Collider>().bounds.extents.y + 0.1f;
Ray ray = new Ray ();
ray.origin = rayOrigin;
ray.direction = Vector3.down;
if(Physics.Raycast(ray, rayDistance, 1 << 8)) {
Debug.Log ("Jumping");
GetComponent<Rigidbody>().AddForce (Vector3.up * JumpForce, ForceMode.VelocityChange);
}
}
}