- Home /
Need help! Enemy follows player with its head!
I have a huge problem. The enemy chases after the player headfirst. I have spent all morning (a full five hours) trying to fix this problem. I have tried to fix it with code, inspector properties, and even by reimporting the model from 3ds max. With the destruction of my ego, I have found no solution; therefore, I am hoping for a miracle that someone has had a similar problem and has found a solution. Here's my code just incase:
#pragma strict
var detectedCheck : EnemyVolumeOne;
var willAttackCheck : boolean;
var target : Transform;
var moveSpeed = 4;
var damping : float = 4;
function Update () {
detectedCheck = GetComponentInChildren(EnemyVolumeOne);
if(detectedCheck.detected == true){
transform.LookAt(target);
//player looks at me with wrong angle, i have the follow script right, but i need the enemy to follow me correctly.
//IGNORE
/*var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;*/
//transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
//transform.LookAt(target);
/*var rotation = Quaternion.LookRotation(target.transform.position - transform.position);
rotation.x = 0;
transform.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime * damping);*/
//transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime *1);
//transform.Translate(Vector3.up * moveSpeed * Time.deltaTime *-1);
//transform.position += transform.up * moveSpeed * Time.deltaTime *-1;
print("he's gonna get you");
}
}
Answer by Kasigawa · Aug 22, 2013 at 03:43 AM
After struggling all day I finally found a solution! Did some digging and found this!
[QUOTE]It still works for me ;) In Max: "Select rotate pivot only" make your rotation so X= 90 and the rest is 0 0 Export with Y as up.
Bingo.
If you have a Biped: ad a root note that has 90 0 0 in rotation and then link the biped to the root so the root becomes parent (Root)
Bingo he stands. Hope it helps.........[QUOTE]
Answer found here: http://forum.unity3d.com/threads/10343-How-to-get-the-XYZ-axis-right-(3DS-MAX-Unity)/page3
Again thanks for the help.
Answer by robertbu · Aug 21, 2013 at 07:27 PM
People on this list have this problem all the time. I'm surprised you were not able to fix it by rotating it in 3ds Max. When your mesh is in Unity with a rotation of (0,0,0), the side you want to be front needs to be looking towards positive 'z'. Typically there are three fixes:
Fix the rotation of the model in 3ds max.
Use an empty game object with your model as a child. The LookAt() script goes on the empty game object. You then rotate the child, visible object so that it is facing positive 'z' when the empty parent object as a rotation of (0,0,0).
Use an additional rotation to 'fix' things.
For an addition rotation you would do your look something like:
transform.rotation = Quaternion.Euler(-90,0,0) * Quaternion.LookRotation(target.position - transform.position);
I don't know how your model is currently rotated, so the 'Quaternion.Euler() is just a placeholder. You would need to experiment with different values until your enemy is facing the correct direction.
It now falls through the floor. Could you tell me how to change these properties in 3dsmax?
It likely falls through the floor because either 1) you don't have a mesh collider on your object or 2) both the floor and the object use mesh colliders and you did not enable the 'Convex' property of the mesh collider on your object. Convex mesh collider are limited to 255 triangles. If mesh collider against mesh collider is the issue, you could also change the floor to use a box collider.
I don't know 3ds max. The problem of importing from 3ds max has come across UA many times. I suggest a Google of: "3ds max unity3d import".
It's good to know that I'm not the only one having this issue. Even though I haven't found a solution, I want to thank you for the help. When I come up with a solution I'll post it to this thread.