【发布时间】:2018-05-31 00:28:51
【问题描述】:
我正在构建一个由玩家移动的 3D 迷宫。当玩家移动迷宫时,我正在采用“迷宫”方法并在迷宫中操纵一个小球。当球员沿球的方向移动球当前所在的墙时,就会出现问题。球穿过墙壁并停在下一个可用的墙壁上。
迷宫使用网格碰撞器和刚体,球是球体碰撞器。
我大幅提高了物理帧速率,但无济于事。考虑到迷宫的复杂性和潜在的大量迷宫组合,我真的不想将简单的对撞机附加到每面墙上。任何提示、技巧、建议、cmets 等将不胜感激。
连续旋转的球脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ballGravity : MonoBehaviour {
public Rigidbody m_Rigidbody;
public Vector3 m_EulerAngleVelocity;
// Use this for initialization
void Start () {
m_EulerAngleVelocity = new Vector3 (0, 100, 0);
m_Rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
Quaternion deltaRotation = Quaternion.Euler (m_EulerAngleVelocity * Time.deltaTime);
m_Rigidbody.MoveRotation (m_Rigidbody.rotation * deltaRotation);
}
}
迷宫旋转脚本:
using UnityEngine;
using System.Collections;
public class rotObj : MonoBehaviour
{
private float baseAngle = 0.0f;
float rotSpeed = 10;
void OnMouseDown(){
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) *Mathf.Rad2Deg;
}
void OnMouseDrag(){
//float rotY = Input.GetAxis("Vertical")*rotSpeed*Mathf.Deg2Rad;
//gm.transform.Rotate(Vector3.right, rotY);
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2(pos.y, pos.x) *Mathf.Rad2Deg - baseAngle;
transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
}
}
【问题讨论】:
-
你如何移动你的球?你是在设置它的位置还是在使用刚体的
MovePosition方法?你应该显示一些代码。 -
谢谢您先生,正如您现在所问的那样,我正在使用刚体。MoveRotation 与球一起帮助我避免球停止但穿过迷宫网格仍然在这里,当我快速旋转迷宫时会发生这种情况。
-
不要旋转(平移或缩放)网格对撞机。重新计算它们的成本很高。此外,如果您通过变换设置旋转,也会遇到同样的问题。
-
那么我可以用网格对撞机旋转迷宫的逻辑做什么,因为如果我不旋转迷宫,那么我必须改变游戏逻辑意味着将控制器添加到我不感兴趣的球上。
标签: c# unity3d game-physics