Basically I have an enemy that’s can move around the map. There’s a bunch of buildings, (71),the nearest ones in an array called Gos,which is then sorted. then it Instantiates a grapplehook prefab called dem1hook and renamed to D1hook. I want that hook (which moves forward from its own, different script) to be aimed at the second-closest gos, so I used the LookAt function or LookRotation (which give different, both incorrect results) in different ways and none of them worked. I’m lost.
here’s the instantiating/aiming Method of the script that spawns the hook:
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class D1grapplespawn : MonoBehaviour
{
public GameObject[] gos;
public GameObject closest;
public Vector3 closestloc;
public GameObject[] ya;
public GameObject D1hook;
public GameObject dem1hook;
public GameObject dem1;
public GameObject LeftCheck;
lefthookcheck lefthookcheck;
public GameObject lol;
public float distTween;
public float mindist;
float hook;
public float lasted;
public float hooks;
List<GameObject> someList;
public LayerMask mask;
D1hookend1 D1hookend1;
void Start()
{
dem1 = GameObject.Find("demon1");
LeftCheck = GameObject.Find("lefthookcheck");
lefthookcheck = LeftCheck.GetComponent<lefthookcheck>();
}
void Update()
{
if (lol)
{
distTween = Vector3.Distance(lol.transform.position, transform.position);
if (distTween > mindist && D1hookend1.attached == true)
{
Destroy(dem1hook.gameObject);
}
if (D1hookend1.hitbound == true)
{
hooks = hooks - 1;
(lol.GetComponent(typeof(D1hookend1)) as D1hookend1).KillHook();
}
}
if (D1hookend1.attached == true)
{
(dem1.GetComponent(typeof(demonai)) as demonai).flingtoHook();
}
if (hooks == 0)
{
SpawnHook();
}
lol = D1hook;
if (D1hookend1.attached == true)
{
lasted = lasted + 1;
}
if (lasted == 20)
{
D1hookend1.attached = false;
Destroy(lol.gameObject, 0);
hooks = hooks - 1;
lasted = 0;
}
}
void SpawnHook()
{
gos = Physics.OverlapSphere(transform.position, 6000, mask)
.OrderBy(x => Vector3.Distance(x.transform.position, transform.position))
.Select(x => x.gameObject).ToArray();
closest = gos[1];
closestloc = gos[1].transform.position;
Quaternion lookat = Quaternion.LookRotation(gos[1].transform.position - transform.position);
transform.rotation = lookat;
D1hook = Instantiate(dem1hook, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
lol = D1hook;
D1hookend1 = D1hook.GetComponent<D1hookend1>();
hooks = hooks + 1;
}
}
}
The problem is that I can’t figure out why closestloc isn’t returning the right Vector. I tried putting gos[1] in, instead, but that didn’t work either. I tested the scene by adding " D1hook.transform = “closestloc” and it gave a completely different position from where “closest” is. What am I doing wrong, and how can I get my grapplehook to shoot towards the second closest “gos”?