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 twoface262 · Apr 10, 2012 at 02:49 AM · rotationjavascriptaxis

How to lock Y Rotation

Hello, I'm trying to make an ai code that follows and rotates towards the player. The code I'm using is.

 var rotation = Quaternion.LookRotation(player.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damp);



Is there anyway to lock the Y axis or make sure it doesn't rotate on the Y axis? thanks!

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

3 Replies

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

Answer by OzzyRawrz · Apr 10, 2012 at 04:02 AM

Make a new empty Object with a box collider and set the size of the collider for the distance you want to start the script on colliding with the Player. place the empty game object in the Enemy you made as a children and try this on the EmptyObject it will work.

 var Myself : Transform; 
 var WalkSpeed : float;
 var Player : Transform;
 var PlayerAlert : int;
 function Start () {
 
 }
 
 function Update () {
        if(PlayerAlert == 1){
             Myself.rotation = Quaternion.LookRotation(Vector3(Player.position.x, Myself.position.y, Player.position.z) - Myself.position);
             Myself.transform.position += Myself.transform.forward * WalkSpeed * Time.deltaTime;  
 }
 
 function OnTriggerEnter(other : Collider){
        if(other.gameObject.CompareTag("Player")){
 
               PlayerAlert = 1;
        }
 }

it will follow the player....set the myself to the main enemy object. and put a tag "player" on the object you want this to follow.

Comment
Add comment · Show 2 · 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 twoface262 · Apr 10, 2012 at 08:39 PM 1
Share

This honestly has nothing to do with my question. This is just a peace of code, I already have my AI system done. What I'm asking is how to make sure the AI doesn't not rotate on the Y Axis...

avatar image OzzyRawrz · Apr 10, 2012 at 11:05 PM 0
Share

Use this code...

$$anonymous$$yself.rotation = Quaternion.LookRotation(Vector3(Player.position.x, $$anonymous$$yself.position.y, Player.position.z) - $$anonymous$$yself.position);

avatar image
0

Answer by BiG · Apr 10, 2012 at 09:46 PM

If you want to lock the y-axis rotation for all the duration of the game, look at the Inspector of your object on which that script is attached. Go under Rigidbody->Constraints->Freeze Rotation. At that point, just check the "Y" box.

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 by0log1c · Apr 10, 2012 at 10:36 PM

Well I had written thislast night and deleted because it felt out-of-place. Its still not clear if its what you're looking but... here. What I often do is create a helper function that returns any position with my own Y, so my actors keep their feet on the ground, JS example:

 function CleanPosition(position:Vector3):Vector3
 {
     position.y = transform.position.y;
     return position;
 }

Then anytime you LookAt() or Move(), you should send the vector3 through this method. It might not blend so well into your already written AI, but perhaps it will. Its simple and worked well for me.

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

7 People are following this question.

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

Related Questions

Cube Rotation Problem 0 Answers

How to Rotate on one axis? 4 Answers

Problem changing 3rd Person controller rotation 1 Answer

Character rotation on foot? 1 Answer

Need to change direction of transform? 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