我有一个汽车游戏,只能沿着x和z轴行驶。 如果汽车驶至正z(旋转== 0),则它必须做一些事情;如果汽车驶向正x(旋转== 90),则它必须做其他事情,依此类推。
<script>
$(document).ready(function() {
$('#companies-index-table').DataTable({
serverSide: true,
processing: true,
responsive: true,
ajax: "{{ route('admincompanies.datatables') }}",
columns: [
{ name: 'id' },
{ name: 'name' },
{ name: 'created_at' },
{ name: 'updated_at' }, <-- I was missing this line so my columns didn't match the thead section.
{ name: 'count', orderable: false },
],
});
});
</script>
它适用于北部和东部,但不适用于南部和西部。即使我以旋转== -180 ||的汽车来启动程序180。 我究竟做错了什么? 谢谢!
答案 0 :(得分:2)
You need to use non-negative value, as the documentation states, it must be between 0 and 360.
if (transform.rotation.eulerAngles.y == 180)
{
richtung = 2;
Debug.Log("south");
}
if (transform.rotation.eulerAngles.y == 270)
{
richtung = 3;
Debug.Log("west");
}
You had the third case almost right, but unfortunately you used transform.rotation.y
instead of transform.rotation.eulerAngles.y
.
As @Everts pointed out, it would be better to compare the values with a "epsilon" value, because float
and double
are not perfectly precise (because of the format in which they are stored in memory).
Math.Abs(transform.rotation.eulerAngles.y - 180) < 0.001
答案 1 :(得分:2)
由于您仅将车辆设置为四个方向,因此您可以同时设置更多方向。
让我们考虑使用A和D键旋转车辆,并且车辆从richtung = 0开始。
public enum Direction { North, East, South, West }
public Direction VehicleDirection{ get{ return (Direction)richtung; } }
private int richtung = 0;
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
if(++richtung == 4){ richtung == 0; }
}
else if(Input.GetKeyDown(KeyCode.D))
{
if(--richtung < 0){ richtung == 3; }
}
}
现在您无需关心轮换,因为您现在可以使用richtung的价值进行轮换。在其他任何地方,您都可以使用VehicleDirection来更明确地指示其前进的方向。