Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by vic485 · Jul 15, 2014 at 09:48 AM · playermodelwrong

Enemy Model Faces wrong direction

I have a model of a wolf in game that is "supposed to" face the character when he comes within a certain distance, and then chase him as he gets closer. Unfortunately the model seems to face away from the character upon entering said zone. Is there anyway to adjust this through the script?

 var Distance;
 var Target : Transform;
 var lookAtDistance = 25.0;
 var chaseRange = 15.0;
 var attackRange = 1.5;
 var moveSpeed = 5.0;
 var Damping = 6.0;
 var attackRepeatTime = 1;
 
 var theDammage = 10;
 
 private var attackTime : float;
 
 var controller : CharacterController;
 var gravity : float = 20;
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Start(){
     anim = GetComponent("Animator");
     attackTime = Time.time;
 }
 
 function Update (){
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance){
         lookAt();
     }
     
     if (Distance > lookAtDistance){
 
     }
     
     if (Distance < attackRange){
         attack();
     }
     
     else if (Distance < chaseRange){
         chase();
     }
 }
 
 function lookAt (){
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function chase (){
     moveDirection = transform.forward;
     moveDirection *= moveSpeed;
     
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }
 
 function attack(){
     if (Time.time > attackTime)
         Target.SendMessage("ApplyDammage", theDammage, SendMessageOptions.DontRequireReceiver);
         attackTime = Time.time + attackRepeatTime;
     }
 }
 
 function ApplyDamage (){
     chaseRange += 30;
     moveSpeed += 2;
     lookAtDistance += 40;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Anti-Social Fred · Jul 15, 2014 at 11:32 AM

     void LookAtUnit(Vector3 direction)
     {
          direction = new Vector3(direction.x, _t.tTransform.position.y, direction.z); // DON'T change the Y rotation in this instance, because that would be pretty ugly
         _t.tTransform.rotation = Quaternion.Slerp(_t.tTransform.rotation, Quaternion.LookRotation(direction - _t.tTransform.position), movement.rotationSensitivity * Time.deltaTime);
     }

_t.tTransform is the player's Transform

Pretty self-explanatory but this rotates the character to face another character, where direction is the direction of the target character. The actual slerp isn't used correctly here, but it gives a nice consistent turn speed to the character.

If this doesn't work, then it means that your model has the wrong forward. To clear up any potential confusion, the forward is the blue arrow in Unity. You should always see that coming out of the unit's front, and if it isn't then you should export the model again, except rotated properly. It sounds like LookAt() isn't working because of this reason, but I decided to throw in my Quaternion solution because I haven't used LookAt() since.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Creating a 3rd Person Character (Not a modeling question) 1 Answer

hands rotating with steering wheel 1 Answer

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

Allow player to import 3D model for character 0 Answers

Player movement wrong akse 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges