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 CrilleStyles · Feb 26, 2015 at 01:18 PM · javascriptailookat

AI Lookat script Only using the z axis

How can I make an AI lookat script with only the z axis rotating?

 var target;
 
 function Start()
 {
 target = GameObject.CompareTag("Player"); // this might be wrong cuz I wrote it on my phone
 }
 
 function Update()
 {
 //Look at
 }
 
Comment
Add comment · Show 1
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 CrilleStyles · Feb 26, 2015 at 05:02 PM 0
Share

any ideas?

2 Replies

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

Answer by DanSuperGP · Feb 26, 2015 at 07:34 PM

 Vector3 toTargetVector = target.position - transform.position;
 float zRotation = Mathf.Atan2( toTargetVector.y, toTargetVector.x )*Mathf.Rad2Deg;
 transform.rotation = Quaternion.Euler(new Vector3 ( 0, 0, zRotation);

This points the X axis at the target... if you want to point the Y axis at the target replace the second line with ...

 float zRotation = ( Mathf.Atan2( toTargetVector.y, toTargetVector.x )*Mathf.Rad2Deg ) -90.0f;
Comment
Add comment · Show 3 · 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 CrilleStyles · Feb 27, 2015 at 04:09 PM 0
Share

Thank you! :)

avatar image RingK · Feb 08, 2017 at 06:57 PM 0
Share

$$anonymous$$an, almost 2 year later, I love you! I spent all day looking for a solution, can't find it until now! You're the man!

avatar image angelonit · Jun 16, 2020 at 09:24 AM 0
Share

Still super useful :)

avatar image
1

Answer by siaran · Feb 26, 2015 at 06:05 PM

Maybe something like:

 Vector3 target;
 
 void Update(){
 
  transform.LookAt(target);    
  transform.rotation.eulerAngles = new Vector3(0,0, transform.rotation.eulerAngles.z);    
 }
Comment
Add comment · Show 4 · 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 CrilleStyles · Feb 26, 2015 at 06:51 PM 0
Share

I wrote it something like this and it gives me errors. I'm pretty new to C# :P

 using UnityEngine;
 using System.Collections;
 
 public class AILookat : $$anonymous$$onoBehaviour {
 
     public Transform target;
     public Vector3 targetpos;
 
     void Start () {
         targetpos = target.transform.position;
     }
     
 
     
     void Update(){
         
         transform.rotation = transform.LookAt(target);    
         transform.rotation.eulerAngles = new Vector3(0,0, transform.rotation.eulerAngles.z);    
     }
 }
 
avatar image siaran · Feb 26, 2015 at 06:57 PM 0
Share

...what errors?

edit: You should do transform.LookAt(targetpos) or transform.LookAt(target.position) ins$$anonymous$$d of transform.LookAt(target), since you have target as a Transform rather than a Vector3.

avatar image CrilleStyles · Feb 26, 2015 at 07:00 PM 0
Share

got these errors AILookat.cs(16,27): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.rotation'. Consider storing the value in a temporary variable AILookat.cs(15,27): error CS0029: Cannot implicitly convert type void' to `UnityEngine.Quaternion'

avatar image siaran · Feb 26, 2015 at 07:17 PM 0
Share

Oh, right, you don't need to say transform.rotation = transform.LookAt(target), ins$$anonymous$$d just do

transform.LookAt(target), will edit in my answer.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to detect if transform.lookat is going through walls? 1 Answer

Zombie AI Help 2 Answers

Look at and animal ai 0 Answers

Make LookAt only rotate X Axis 1 Answer

look at target problem 2 Answers


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