0
\$\begingroup\$

I'm using ThirdPersonController as player i also tried with a cube.

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;
    }
}

Inside the FindDirections bottom i'm making the player to be facing to the selected direction:

if (player.position == start.position)
    player.forward = direction;

Then in the Update i'm moving the player by 1.5 fistance on the selected direction.

It's working fine on the 3 directions: Left , Right , Backward

But when it's selecting the forward direction i see the player is moving right. I tried many times and it's never moving forward on the grid.

I can't figure out why it's not moving forward it does selecting the forward direction sometimes but it's not making the player moving forward on the grid.

In the screenshot it's example of a grid the blue circle is the player the red arrows are what the player is moving and the black arrow should be forward but it's never moving that direction. When it's forward it's moving to the right but never to the black arrow direction as forward should be:

Grid example

\$\endgroup\$
1
\$\begingroup\$

Unity Documentation: Random.Range

public static int Range(int min, int max); Description

Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).

Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9.

You're off by one on your Random.Range call, so forward never gets selected.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.