我创建了一个脚本,以便我可以通过按A来克隆士兵,然后士兵移动到选中时点击的位置。我已经测试了没有克隆士兵的脚本,一切正常。 不幸的是,克隆人不起作用,我怀疑navamesh,但我真的不知道。有人可以帮帮我吗 : 这是我的脚本:
产生克隆的第一个:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Placement_controller : MonoBehaviour {
[SerializeField]
private GameObject placeableObjectPrefab;
[SerializeField]
private KeyCode newObjectHotkey = KeyCode.A;
public GameObject currentPlaceableObject;
private void Update()
{
HandleNewObjectHotkey();
if (currentPlaceableObject != null)
{
MoveCurrentObjectToMouse();
ReleaseIfClicked();
}
}
private void HandleNewObjectHotkey()
{
if (Input.GetKeyDown(newObjectHotkey))
{
if (currentPlaceableObject != null)
{
Destroy(currentPlaceableObject);
}
else
{
currentPlaceableObject = Instantiate(placeableObjectPrefab);
}
}
}
private void MoveCurrentObjectToMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
}
private void ReleaseIfClicked()
{
if (Input.GetMouseButtonDown(0))
{
currentPlaceableObject = null;
}
}
}
第二个选择单位:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;
public class SelectUnit : MonoBehaviour{
public GameObject selectedunit;
RaycastHit hit;
void Update()
{
if (selectedunit == null)
{
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
if (hit.transform.tag == "SelectableUnit")
{
selectedunit = hit.transform.gameObject;
selectedunit.transform.Find("Marker").gameObject.SetActive(true);
}
}
}
}
else
{
if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift))
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
if (hit.transform.tag == "SelectableUnit")
{
selectedunit.transform.Find("Marker").gameObject.SetActive(false);
selectedunit = null;
selectedunit = hit.transform.gameObject;
selectedunit.transform.Find("Marker").gameObject.SetActive(true);
}
if (hit.transform.tag == "Floor")
{
selectedunit.transform.Find("Marker").gameObject.SetActive(false);
selectedunit = null;
}
}
}
}
}
}
最后一个移动到选中时鼠标点击的位置:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;
public class Unit : MonoBehaviour{
public GameObject selectunit;
private NavMeshAgent agent;
private RaycastHit hit;
public GameObject self;
public GameObject manager;
public bool mRunning = false;
private Animator mAnimator;
public Vector3 offset;
void Start()
{
agent = GetComponent<NavMeshAgent>();
mAnimator = GetComponent<Animator>();
}
void Update()
{
agent = GetComponent<NavMeshAgent>();
mAnimator = GetComponent<Animator>();
selectunit = manager.GetComponent<SelectUnit>().selectedunit;
if (manager.GetComponent<SelectUnit>().selectedunit == self)
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.transform.tag == "Floor")
{
agent.destination = hit.point + offset;
}
}
}
}
if (agent.remainingDistance <= agent.stoppingDistance)
{
mRunning = false;
}
else
{
mRunning = true;
}
mAnimator.SetBool("running", mRunning);
}
}
Clone's navmesh Normal soldier's navmesh 对我来说两者完全相同。