|
I made and animated a simple model in 3DS Max and exported with Y direction being up. Everything seems to work just fine, except when I replaced my capsule placeholder with the model, he chases me ass first. ;( I've tried rotating the model, but as soon as he locks on to my position, he rotates around again and chases me with his backside. I'm a noob, so any help would be greatly appreciated!
(comments are locked)
|
|
I believe that one of two things happened.
To fix the model in Max, select all the verts (not the entire model -- make sure you are in vert select mode and select all of them.) Then rotate them 180 on Y so it faces +Z (Z is the blue arrow in Max, and in Unity.) In Blender you can start a Y-spin and then type "180," to get it exact. Rotating the model, or using the export settings will change the Transform, which your script will overwrite.
Sep 04 '11 at 07:37 PM
Owen Reynolds
(comments are locked)
|
|
You're probably assigning a new rotation to the enemy when it finds you, what replaces the one you've set at the Editor. If you're using a CharacterController, drag the model to your enemy (child the model to it) and adjust the model orientation - the model will keep its relative rotation when the enemy turns to follow you. But if it doesn't work, then probably the problem is in your script: post it here and we will check what's wrong. EDITED: This is the code modified to go directly to the player while rotating to face it (you were going in the enemy forward direction while rotating to the player, what can give crazy results). If the enemy has a rigidbody, freeze rotations.
var Player : Transform;
var chaseDistance: float = 30;
private var MoveSpeed : float = 0.3;
private var RotationSpeed : float = 0.5;
function Update ()
{
var playerDir = Player.position-transform.position;
if (playerDir.magnitude < chaseDistance){
//Moving the character to the player
transform.position += playerDir.normalized * MoveSpeed * Time.deltaTime;
//Face the player - using Vector3.Lerp is easier because we already have the dest vector
transform.rotation = Vector3.Lerp(transform.forward, playerDir, RotationSpeed * Time.deltaTime);
}
}
(comments are locked)
|
|
Here's the code I use, it's very simple. Maybe it's something todo with how I'm exporting it from 3DS Max? var Player : Transform; You are moving in the enemy forward direction, but at the same time you're turning to the player, what can produce crazy results. Find the player direction, then walk in this direction while rotating to face the player. I included here a chaseDistance - the enemy will do nothing while the player is too far. You can remove this, or set chaseDistance to 10000 to make the enemy always chase the player.
var Player : Transform;
var chaseDistance: float = 30;
private var MoveSpeed : float = 0.3;
private var RotationSpeed : float = 0.5;
function Update ()
{
var playerDir = Player.position-transform.position;
if (playerDir.magnitude<chaseDistance){
//Moving the character to the player
transform.position += playerDir.normalized * MoveSpeed * Time.deltaTime;
//Face the player - using Vector3.Lerp is easier because we already have the dest vector
transform.forward = Vector3.Lerp(transform.forward, playerDir, RotationSpeed * Time.deltaTime);
}
}
NOTE: I fixed an error in the last line (transform.forward was written transform.rotation...)
Sep 04 '11 at 07:45 PM
aldonaletto
(comments are locked)
|
|
Thanks guys, Parenting it to a empty game object, and applying the scripts to the GO instead of the model itself seems to fix it a little bit getting the rotation to line up correctly is a pain. I'm now having other troubles, like after awhile of chasing, the model goes crazy and starts clipping through the ground while spinning like crazy. I locked it's rotation under it's rigidbody, but no avail. Does anyone know a fix for this? I dont really like these hokey pokey work arounds. I would very much like to know how to export this properly =D Hey all, I tried your script Aldonaletto, but it doesn't work. Gives this as a error: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'. I tried rotating the vertices, but that didn't seem to work for me. ( maybe I did it wrong :x ) I loaded up a model from a pack I bought and exported it. And the script works perfectly with it, it chases face first. Which leads me to believe that something is wrong with my model? I'm still learning 3DS Max, and Unity. So I know I'm screwing something up somewhere. I thank you all for your help, it's greatly appreciated!
Sep 05 '11 at 01:02 PM
NellacKun
My fault! I used transform.forward instead of transform.rotation, but forgot to alter the left side of the last statement! I fixed it in the code, at the other comment. Give it a try if you have problems with your algorithm.
Sep 05 '11 at 03:55 PM
aldonaletto
Thanks for the update, works great now. But unfortunately, my model still goes toward my player backside first. :(
Sep 05 '11 at 04:18 PM
NellacKun
Well, I think the only way is to child your model to the enemy object, and adjust it's rotation at the Inspector to (0, 180,0)
Sep 05 '11 at 04:59 PM
aldonaletto
(comments are locked)
|

LOL, LMAO.
It is pretty hilarious looking. But I really need to find out why it's doing it. :)