C#笔记6//建造游戏2/鼠标点击建造脚本1/自编/无需插件/仅建造

上一个脚本那个网格就当个辅助工具吧,拿来看点用的

这是一个测试用脚本,检测该基本功能的实现

这个脚本可以在点击到的网格块顶点生成预制件,初步实现了网格建造功能

内容比较简单,上代码

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

///-----------------------------------------------------------------------------------------          
///   Founction:	            Build the cylinder on grid by clicking
///   InvolvingKnowledge:      	Ray
///   Author:               	AttouchAusturo
///   LatestReviseTime:	        2021.12.18
///-----------------------------------------------------------------------------------------

public class GetGroundAreaByClicking : MonoBehaviour
{
    //射线获取坐标
    private Ray ray;
    private RaycastHit hit;
    private Vector3 target;//鼠标点击位置的世界坐标

    //坐标转换
    private Vector3 groundArea;

    //要建造的预制件
    public GameObject cylinderPrefab;
    public GameObject buildingFounctionTest;

    private void Update()
    {
        //当点击鼠标左键时
        if (Input.GetMouseButtonDown(0))
        {
            //鼠标在屏幕的位置
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                //绘制出一条从相机射出的红色射线
                Debug.DrawLine(Camera.main.transform.position, hit.point, Color.red);
            }
            target = hit.point;//获取点击位置的世界坐标

            groundArea = new Vector3((int)target.x / 10 * 10, 0, (int)target.z / 10 * 10);//坐标取整
            buildingFounctionTest = Instantiate(cylinderPrefab, groundArea, Quaternion.identity) as GameObject;//在格子顶点生成该物体
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值