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 KM Studios · Aug 24, 2012 at 01:39 PM · c#vector3rotateyrestrict

Rotate towards target with limitation on certain axis? c#

Ok so I have this c# script to get my enemy to look at my player, but how can I add to this script to restrict the Y axis rotation? I tried a few other scripts i found online but they didnt seem to work right. Thank you!

 using UnityEngine;
 
 public class rotating: MonoBehaviour
 {
     //values that will be set in the Inspector
     public Transform Target;
     public float RotationSpeed;
 
     //values for internal use
     private Quaternion _lookRotation;
     private Vector3 _direction;
 
     // Update is called once per frame
     void Update()
     {    
         
        //find the vector pointing from our position to the target
        _direction = (Target.position - transform.position).normalized;
 
        //create the rotation we need to be in to look at the target
        _lookRotation = Quaternion.LookRotation(_direction);
 
        //rotate us over time according to speed until we are in the required rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
     }
 }
Comment
Add comment · Show 2
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 Punkjim420 · Aug 24, 2012 at 02:22 PM 0
Share

i dont know much c# but if your enemy has a rigidbody you can select which axis it will be allowed/not allowed to rotate/translate on.

avatar image anonymousUser · Aug 24, 2012 at 02:25 PM 0
Share

i posted a suggestion, but still pending... try :

 void Update()
 {   
   Vector3 a = transform.eulerAngles;

    //find the vector pointing from our position to the target
    _direction = (Target.position - transform.position).normalized;

    //create the rotation we need to be in to look at the target
    _lookRotation = Quaternion.LookRotation(_direction);

    //rotate us over time according to speed until we are in the required rotation
    transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);


   transform.eulerAngles.x = a.x;
   transform.eulerAngles.z = a.z;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by anonymousUser · Aug 24, 2012 at 03:09 PM

can you do this?

     void Update()
     {   
 
        Vector3 a = transform.eulerAngles;
 
        //find the vector pointing from our position to the target
        _direction = (Target.position - transform.position).normalized;
 
        //create the rotation we need to be in to look at the target
        _lookRotation = Quaternion.LookRotation(_direction);
 
        //rotate us over time according to speed until we are in the required rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
 
        transform.eulerAngles.x = a.x;
        transform.eulerAngles.z = a.z;
 
     }
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 Arshia001 · Aug 24, 2012 at 02:32 PM

try adding this after _lookRotation = ...


Vector3 v = _lookRotation.eulerAngles;
v.x = 0;
_lookRotation.eulerAngles = v;

this code sets the X rotation of the euler angles to zero, which means you'll get no rotation around X. Since the quaternion is looking at your player/object/whatever, the Z axis is pointing in that way and if you remove the rotation around X, the enemy will stay upright. If you want to restrict another axis, change the x to whatever you need.

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 KM Studios · Aug 24, 2012 at 03:48 PM 0
Share

Idk for some reason the enemy is really glitchy, to the point where if I touch him I go flying across the map. Is it that the nav mesh agent component does it's own rotation? This is the pro version, but the AI following on it was really bad, the enemy would slide around and take forever to turn towards me, so I wanted to have him face me with this rotation script but nothing seems to work, it only makes him glitch more.

avatar image KM Studios · Aug 24, 2012 at 04:10 PM 0
Share

Ok I got it, the nav mesh agent component's built in rotation suck but it must have interfered with my c# script. So I just had to adjust some variables to the nav mesh agent for it to rotate faster, and the Y axis is never affected. I wish it was more customizable, but the AI navigation is really good compared to anything else I've used. Thanks for all the help you guys I'm sure those scripts work just not at the same time as another one, which probably caused the glitching.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Trouble with Vector3.Angle() and RotateAround() 1 Answer

Distribute terrain in zones 3 Answers

Rotating object around Z axis with lerp/slerp 1 Answer

Rotate Animation In-Game? 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