-1

I am trying to make an empty game object which is the path generator rotate when placing a tile left or right.

But somehow it does not rotate the object.

Plz help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathGenerator : MonoBehaviour
{
    #region Settings

    //Settings to customize this script

    float gapSize = 0.4f; //0,4 coord gaps between squares

    #endregion

    public GameObject ChosenPath;
    public GameObject NonChosenPath;

    private Vector3 InitPos;
    private Vector3 NextPos;

    public Vector3 angleRotation;

    public string pathDirection;

    // Start is called before the first frame update
    void Start()
    {
       InitPos = new Vector2(gapSize,gapSize);
       NextPos = InitPos;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            ChangePath();
        }
    }

    void ChangePath()
    {
        double randomNumQ = Random.Range(0,3);
        if(randomNumQ == 0)
        {
            pathDirection = "Forward";
            NextPos.y += gapSize;
        }
        else if(randomNumQ == 1)
        {
            pathDirection = "Left";
            NextPos.x += gapSize;
            angleRotation.z = -90; 
            transform.Rotate(angleRotation);
        }
        else if(randomNumQ == 2)
        {
            pathDirection = "Right";
            NextPos.x -= gapSize;
            angleRotation.z = 90; 
            transform.Rotate(angleRotation);
        }
        Instantiate(ChosenPath, NextPos, transform.rotation = Quaternion.identity);
        transform.position = NextPos;
    }
}
rfmodulator
  • 3,638
  • 3
  • 18
  • 22
  • You call `ChangePath` **every frame** while holding down the space key ... I guess you would rather want to use `GetKeyDown` in order to do it only **once** per key press... – derHugo Apr 02 '22 at 07:35

1 Answers1

0

Actually the problem was that the Instantiate was after and it resets the rotation of the object