我正在使用ThirdPersonController作为播放器,我也试过了一个立方体。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;
public class DirsFinder : MonoBehaviour
{
enum Directions
{
Forward,Backward,Left,Right
}
public Transform player;
private Transform start;
private Transform end;
private GridGenerator gridgenerator;
public List<Vector3> possibleDirections = new List<Vector3>();
private Vector3 direction;
private float m_distanceTraveled = 0f;
public void Find()
{
gridgenerator = GetComponent<GridGenerator>();
GenerateStartEnd();
FindDirections();
m_distanceTraveled = 0;
}
private void FindDirections()
{
possibleDirections = new List<Vector3>();
Vector3 playerPosition;
playerPosition = player.localPosition;
if (playerPosition.x > 0)
{
// can go left
possibleDirections.Add(Vector3.left);
}
if (playerPosition.x + 1.5f < gridgenerator.gridWidth * 1.5f)
{
// can go right
possibleDirections.Add(Vector3.right);
}
if (playerPosition.z > 0)
{
// can go backward
possibleDirections.Add(Vector3.back);
}
if (playerPosition.z + 1.5f < gridgenerator.gridHeight * 1.5f)
{
// can go forward
possibleDirections.Add(Vector3.forward);
print("I'm moving forward !!!!!");
}
direction = possibleDirections[Random.Range(0, possibleDirections.Count - 1)];
if (player.position == start.position)
player.forward = direction;
}
private void Update()
{
if (m_distanceTraveled < 1.5f)
{
Vector3 oldPosition = player.position;
player.position += direction * Time.deltaTime * 1f;
m_distanceTraveled += Vector3.Distance(oldPosition, player.position);
}
}
private List<Vector3> GenerateStartEnd()
{
GameObject walls = GameObject.Find("Walls");
List<Transform> wallsParents = new List<Transform>();
List<Vector3> startEndPos = new List<Vector3>();
foreach (Transform child in walls.transform)
{
wallsParents.Add(child);
}
for (int i = 0; i < 2; i++)
{
wallsParents.Remove(wallsParents[Random.Range(0, wallsParents.Count)]);
}
var childsWall0 = wallsParents[0].GetComponentsInChildren<Transform>().ToList();
var childsWall1 = wallsParents[1].GetComponentsInChildren<Transform>().ToList();
childsWall0.RemoveAt(0);
childsWall1.RemoveAt(0);
start = childsWall0[Random.Range(0, childsWall0.Count)];
player.position = start.position;
end = childsWall1[Random.Range(0, childsWall1.Count)];
end.tag = "End";
startEndPos.Add(start.position);
startEndPos.Add(end.position);
start.GetComponent<Renderer>().material.color = Color.red;
end.GetComponent<Renderer>().material.color = Color.blue;
return startEndPos;
}
}
在FindDirections底部,我让玩家面向选定的方向:
if (player.position == start.position)
player.forward = direction;
然后在更新中,我将球员移动1.5度距离所选方向。
它在3个方向上正常工作:左,右,后退
但是当它选择前进方向时,我看到玩家正在向右移动。 我尝试了很多次,它从未在网格上前进。
我无法弄清楚为什么它没有前进它有时会选择前进方向,但它并没有让玩家在网格上前进。
在截图中,它是一个网格的例子,蓝色圆圈是玩家,红色箭头是玩家正在移动的东西,黑色箭头应该是向前但是它永远不会移动那个方向。当它向前移动时它向右移动但从未向黑箭头方向移动,因为前进应该是:
答案 0 :(得分:0)
问题:Random.Range行:
direction = possibleDirections[Random.Range(0, possibleDirections.Count - 1)];
解决方案:删除-1
direction = possibleDirections[Random.Range(0, possibleDirections.Count];