0

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

}

}

Not the clone The clone

Clone's navmesh Normal soldier's navmesh both exactly the same for me.

Bruck Film
  • 11
  • 2
  • Have you debugged to see if the cloned units are even getting to the point of the NavMeshAgent destination being assigned? There are a few points where the code is not shown that may be a problem, such as, "are the instantiated Unit objects being given the reference to **manager**"? – avariant Apr 02 '18 at 15:29
  • Please show the inspector for cloned and not cloned as images. – Doh09 Apr 02 '18 at 15:41
  • I put the links to the images of the inspector in the end of the description of my problem i invite you to check it out – Bruck Film Apr 02 '18 at 17:39
  • @avariant Yes the clone automaticly assign the manager because the manager is already asigned to the clone´s reference object ( see the links to the inspector photoes at the end of the problem description ). – Bruck Film Apr 02 '18 at 20:32
  • @BruckFilm can you show the settings of the NavMeshAgent for clone and not clone, thank you. – Doh09 Apr 02 '18 at 21:59
  • @Doh09 Those setting stay exactly the same for both. I don’t think the problem is there – Bruck Film Apr 03 '18 at 04:18
  • 1
    @Doho09 I added the links to the navmesh photos in the end of the description – Bruck Film Apr 03 '18 at 04:35
  • @Doh09 I put the images at the end of the description – Bruck Film Apr 03 '18 at 05:38
  • Do you put the clones and the normal ones in the same location in the scene? – Doh09 Apr 03 '18 at 09:50

0 Answers0