我想问一个问题。首先,我已经可以在玩家撞到墙壁时检测到玩家,但是我无法让玩家穿过墙壁。当玩家撞到墙壁时,我怎么能让玩家停止移动?
这是截图(Red capsule是播放器):
胶囊与棕色墙壁碰撞的第一个图像是来自3ds max的导入对象,我已经通过勾选复选框来应用对撞机,如上图所示。
以下是我正在使用的代码:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class CheckPlayer : MonoBehaviour
{
public float moveSpeed = 5.0f, rotationSpeed = 5.0f; // Define and set for the movement and rotation of the player
private void Update()
{
// Call the Movement and Rotation function
Movement();
Rotation();
}
private void OnTriggerEnter(Collider col)
{
// If the game object collided with the certain tag
if (col.gameObject.tag == "Inn's Objects")
{
// Debug it
Debug.Log("You have collided with the object");
}
// If the game object colided with the certain tag
if (col.gameObject.tag == "Inn's Door")
{
// Load another level
GameManager.LoadLevel("Third Loading Scene");
}
}
private void Movement()
{
// If user press W on the keyboard
if (Input.GetKey(KeyCode.W))
{
// Move the player forward
transform.position -= Vector3.forward * moveSpeed * Time.deltaTime;
}
// But if user press A on the keyboard
else if (Input.GetKey(KeyCode.A))
{
// Move the player to the right
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
// But if user press S on the keyboard
else if (Input.GetKey(KeyCode.S))
{
// Move the player backward
transform.position += Vector3.forward * moveSpeed * Time.deltaTime;
}
// But if user press D on the keyboard
else if (Input.GetKey(KeyCode.D))
{
// Move the player to the left
transform.position -= Vector3.right * moveSpeed * Time.deltaTime;
}
}
private void Rotation()
{
// If user press E on the keyboard
if (Input.GetKey(KeyCode.E))
{
// Rotate the player to the right
transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
}
// But if user press Q on the keyboard
else if (Input.GetKey(KeyCode.Q))
{
// Rotate the player to the left
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
}
}
非常感谢任何帮助!
谢谢
答案 0 :(得分:0)
为什么要使用墙壁触发器?墙壁意味着有colliison,只需使用colliison网。
在旁注中,您可以在碰撞点记录玩家的位置,将移动速度设置为零,然后重置他的位置。