When the player prefab is in the scene he perfectly aims at the cursor, using LookAt, but when I remove the player prefab from the scene and Instantiate him from a script, he now has an offset to his aim.
Why does the clone have an aim offset, but the prefab does not… This seems like a bug with the LookAt.
using UnityEngine;
using System.Collections;
public class FaceMouseOnClick : MonoBehaviour
{
//Components
private SkillsManager s;
//Vairables
public float speed = 2.0f;
public LayerMask layerMask;
void Start () {
s = GetComponent<SkillsManager> ();
}
//Call the method in LateUpdate so we can affect the bones after the animation, otherwise bones can not be translated.
void LateUpdate () {
LookAtTarget();
}
public void LookAtTarget () {
//Raycast from camera to mouse
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
//Plane for Player Rotation
Plane playerPlane = new Plane(Vector3.up, transform.position);
float hitdist = 0.0f;
//Rotate Player
if (playerPlane.Raycast (ray, out hitdist)) {
if (Input.GetMouseButton (0)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.time);
}
}
//Weapon LookAt Cursor
if (Physics.Raycast (ray, out hit, 50.0f, layerMask)) {
float distance = Vector3.Distance (transform.position, hit.point);
if (Input.GetMouseButton (0) && distance > 4.5f) {
s.handHold.LookAt (hit.point);
// Debug.Log ("Distance = " + distance);
}
}
}
}
Correct me If I’m wrong, but this has my programmer and I stumped. Is there a bug with LookAt and Instantiating?
B) The most crucial parts for solving this are going to be your Instantiation code, and the way your prefab is set up versus the way the object was set up when it was in the scene. I’d give pretty good odds that there is some difference in the transform’s values or something like that that would account for an offset.
Thank you for the reply! The prefabs values and the instantiated clone are identical, the only difference is one has an aim offset, with no explanation. I have zeroed it down to this script, I am completely baffled as to why the clone would have an offset all of a sudden, when the prefab dragged directly into the scene does not… which leads me to believe that LookAt has a bug when coupled with an Instantiated object.
There is no difference between the object in the scene and the instantiated one. We discovered the problem with the instantiated object. Dragging the prefrab directly into the scene and running it allows us to use the controls properly while the instantiated one still cannot aim right.
Where do you update the cursors image position? Perhaps your gun is aiming at the correct place, but your cursor image is not. For instance, if the anchor point of your cursor image is not in the center, it will be offset by the corresponding amount
@A.Killingbeck , I already tried that, good idea tho. That is all perfectly working.
@KelsoMRK , Indeed, I’m just frustrated, running out of ideas.
The instantiated clone apparently does not care about this line here, s.handHold.LookAt(hit.point); and I do not know why. When I comment that line out, they both aim with an offset but they both shoot at the same spot. Thank you guys for your responses.
It may actually be this line in the SkillsManager script
currentWeapon = Instantiate(weaponsArray[i],handHold.position,handHold.rotation) as Weapon;
currentWeapon.transform.parent = handHold;
I do not believe the weapon is becoming a child of that transform correctly… the instantiated object should follow the bone handHold but it does not! I am missing something here, but its between these two scripts surely, and those two lines. Any insight about how instantiated objects parent-child relationship works would be helpful as the documentation has not helped me to solve this. Thank you!
You should then consider that any position/rotation you fed into the Instantiate function will have been in global space - you’ll probably want to set the transform.localPosition and localRotation manually after setting the parent. Are you doing that?
@StarManta . thank you for your response, although I’m not sure I follow.
I still wonder why only the clone is offset, while the prefab operates normally. It’s starting to feel like the instantiate method is broken somehow, I made an empty object and made it a child of the handHold bone, he follows the new empty child perfectly… but, that does not explain why he wont follow the handHold bone after instantiation.
Something in the way that instantiate works with bones is somehow bugged. The model is an FBX from blender, exported with Y up just like unity. I just find it odd that it works perfectly without instantiating the prefab, I even tried loading from resources folder bypassing the drag and drop to the Spawner component. Thank you for your time and input guys!
Why do you keep saying it’s a unity bug? ;p It’s 99.99% not a Unity bug, as you are doing something which would normally be extremely trivial, which 1000’s of people have done before and had no problems. Can you post the skillsmanager script aswell?