统一旋转一个空的游戏对象

发布于 2025-01-18 08:55:53 字数 1612 浏览 5 评论 0原文

我正在尝试制作一个空的游戏对象,该对象是路径发生器左右放置瓷砖时旋转的。

但是以某种方式不会旋转对象。

请帮助!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那片花海 2025-01-25 08:55:53

实际上问题是实例化之后并且它重置了对象的旋转

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文