I made a script so that the I could clone soldiers by pressing A and then that the soldier move where a click when they are selected. I've tested the script with not cloned soldiers and everything work. Unfortunatly the clones don't work, i suspect the navamesh but i really don't know. Can someone please help me : Here are my scripts:
The first one to spawn clones :
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;
}
}
}
The second one to select units :
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;
}
}
}
}
}
}
And the last one to move to where the mouse clicks when selected :
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 both exactly the same for me.