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 /
avatar image
0
Question by TheFrankman123 · Mar 21, 2012 at 06:44 PM · aitransform.lookat

transform.LookAt(target);

I am working on enemyAI in which the enemy is on a patrol but engages the player when they are within certain area.

However because the center of my character is lower than the enemy the enemy tries to look down on the floor as i have transform.LookAt(target); telling it to. This makes it act a bit crazy.

Is there a way to use transform.LookAt(target); but specify it to only do so on the Y axis and not the X so it doesn't look down at the floor.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheFrankman123 · Mar 27, 2012 at 11:11 PM

I fixed this in the end, it was complicated but in short 'blacktear' was correct.

I had to two different lookats into the two different states of my enemy. When the enemy is moving toward the waypoints then transform.LookAt(target); was fine

then in my chase state i used:

transform.LookAt(Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));

the reason for this is for some reason using (target) wouldn't work with the both waypoints and the player.

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
avatar image
1

Answer by fafase · Mar 21, 2012 at 07:34 PM

I found this on the other unity forum.

 function LookAtIgnoreHeight (target : Transform) {
     var lookAtPos : Vector3 = target.position;
     //Set y of LookAt target to be my height.
     lookAtPos.y = transform.position.y;
     transform.LookAt (lookAtPos);
 }

That should be what you need.

Please confirm if that works for future consultations.

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
avatar image
0

Answer by blacktear · Mar 21, 2012 at 07:59 PM

hello, yes there is a way to do this, remember that you are calling the transform of the player character, which contains a Vector3.

For example :

transform.LookAt(new Vector3(other.transform.position.x, other.transform.position.y+10, other.transform.position.z));

Hope this helpes :)

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
avatar image
0

Answer by TheFrankman123 · Mar 21, 2012 at 07:59 PM

This throws up the following error:

MissingMethodException: Method not found: 'UnityEngine.Transform.LookAtIgnoreHeight'.

Here is my code. The part is in the Mover Function. Rest of the code is include incase it's relevant.

 var waypoints : Transform [];
 var currentPoint : int = 0;
 var state : String = "patrol";
 var speed : float = 10;
 var chaseDist : float = 15;
 var gravity : float = 15;
 private var player : Transform;
 private var controller : CharacterController;
 
 function Start(){
     player = gameObject.FindWithTag("Player").transform;
     controller = GetComponent(CharacterController);    
 }
 
 function Update () {
     if(player) {    
         var playerDist = Vector3.Distance(player.position, transform.position);
         if (playerDist <= chaseDist ){
             state = "chase";    
         } else {
             state = "patrol";
         }
     
     
         if(state == "patrol"){
             Debug.Log("patrol");
             if(currentPoint < waypoints.length) {
                 Mover(waypoints[currentPoint].position);    
             }
         } else if (state == "chase"){
             Debug.Log("chasing");
             Mover(player.position);
         }
     } 
 }
 
 
 
 function Mover(target : Vector3){
 
     var diffVector: Vector3 = target - transform.position;
 
     var movement : Vector3;
     
     if(diffVector.magnitude > 1){
      movement = (diffVector.normalized * speed);
      
     }else{
         currentPoint = (currentPoint + 1) % waypoints.length;
     }
     
     movement.y -= gravity * Time.deltaTime;
     controller.Move(movement * Time.deltaTime);
     
     
      transform.LookAtIgnoreHeight(target);
     //transform.LookAt(target);
     
 }
 
 function LookAtIgnoreHeight (target : Transform) {
     var lookAtPos : Vector3 = target.position;
     lookAtPos.y = transform.position.y;
     transform.LookAt (lookAtPos);
 }
 
 
 function OnCollisionEnter(hitPlayer: Collision){
     
     if(hitPlayer.gameObject.tag == "Player"){
         Debug.Log("collision detected");
         Destroy(hitPlayer.gameObject);
         Debug.Log("Object Destroyed!");
         
     }
 }
Comment
Add comment · Show 6 · 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
avatar image fafase · Mar 21, 2012 at 08:45 PM 0
Share

try without the transform, actually transform.LookAtIgnoreHeight(target); does not make sense since it is not a member of transform. The guy reported it this way on the thread but as it is simply a function you should call it as a function simply LookAtIgnoreHeight(target);

avatar image fafase · Mar 21, 2012 at 08:49 PM 0
Share

$$anonymous$$y bad, I updated the answer.

avatar image TheFrankman123 · Mar 22, 2012 at 12:28 PM 0
Share

Unless i'm missing something calling the function like this

LookAtIgnoreHeight(target);

in the $$anonymous$$over functions throws up this error...

BCE0017: The best overload for the method 'waypoints2.LookAtIgnoreHeight(UnityEngine.Transform)' is not compatible with the argument list '(UnityEngine.Vector3)'.

avatar image fafase · Mar 23, 2012 at 06:30 AM 0
Share

Try taking off the .position in the parameter you pass in the update, try $$anonymous$$over(waypoints[currentPoint]); and $$anonymous$$over(player); and then change also function $$anonymous$$over (target:Transform){}

avatar image TheFrankman123 · Mar 24, 2012 at 01:33 PM 0
Share

all hell breaks loose when you do that, because then the var diffVector doesn't work and more... surely there is something within the LookAt, maybe LookAtRotation?

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AI script problems, interaction with grenade not working, need help. 2 Answers

Ai Zombie Melee Attack script. 5 Answers

Enemy following Player on uneven surface 1 Answer

About a enemy AI in my game 1 Answer

WayPoints mixed with Raycast 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