Now the thing I’m trying to fix is that the instantiated Gos object isn’t moving toward the right spot. I’m still not sure why. I want it to point toward gos[1] (the second-closest gos) and move forward to collide with it. The hook’s script a simple code, the only part that affects its own transform makes it move forward. I’ve had it so the LookAt vector is gos[1] - transform.positon or also just the gos[1]'s transform. Here’s the aiming-and-instantiating Method:
void SpawnHook ()
{
gos = Physics.OverlapSphere(transform.position, 20000, 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(closestloc - transform.position);
transform.rotation = lookat;
GameObject D1hook = (GameObject)Instantiate(dem1hook, transform.position, transform.rotation) as GameObject;
}
The only code that affects the grapplehook’s own transform in its script is
Srb.AddForce(transform.forward * 10000);
Note: Srb is what I call its Rigidbody.
Note: I get no errors, and the Closest is indeed the second-closest gos[1], as confirmed in the inspector during a paused Play.
what direction is it travelling in?
the instantiated object’s script’s code is:
Srb.AddForce(transform.forward*10000);
It’s actually moving to the forward-left (about the same of forward and left) of the object that is instantiating it. I currently, at its current position, want it to go to the slightly forward and to the right. I want it to collide with gos[1].
Is the spawned object rotated correctly ( towards other object ) ?
It’s just a sphere so I can’t tell. I’m currently setting up a new object so you can help me out.
When you select your object, you can just switch to local space and then see where is the blue axis pointing to. That’s the direction of the force you are applying.
no, the spawned object (the “hook”) isn’t facing the right direction
Problem is then in your rotating code. I don’t know but maybe vector needs to be normalized ?
Quaternion.LookRotation((closestloc - transform.position).normalized);
It didn’t work. It’s really weird eh?
I fidgetted around with my code and now it’s changed to go a lot closer to the target.
void SpawnHook()
{
gos = Physics.OverlapSphere(transform.position, 20000, 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(closestloc);
transform.rotation = lookat;
D1hook = Instantiate(dem1hook, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
hooks = hooks + 1;
}
Now it’s going slightly backwards and to the right. I want it to go barely forward and to the right.
Hmm, is there any additional information that might help?
would it make a difference in the Vector3 “closestloc” where the pivot is, or is it where the collision with the OverlapSphere is?
I did a couple tests with the code to provide more information and pinpoint the problem. They are:
-
additional information: If I move the object with the script attached, the area it instantiated object goes somewhere else (though gos[1] is still the same GameObject), but if I change its rotation nothing changes. In the first example where the position is changed, the instantiated object doesn’t change which way it moves in relation to the object that instantiates it (which the script is on).
-
I tested what would happen if I made a Vector3 out of the difference in position between the object that instantiates the hook and gos [1], and then divided the Vector, and it nothing changed.
both of these seem to indicate that the problem is the the “closestloc” vector doesn’t match the gos[1] position. I tried putting in the gos[1] position instead of closestloc, but that didn’t work. It’s weird.