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 Babilinski · Nov 07, 2011 at 09:52 PM · c#rotationlookface

C Sharp Rotate

I need help.

I have a targeting script and I want my player to rotate and look ato the enemy when targeted. How would I do it. if my enemy is a transform.

I want to be able to toggle on and off this code as well.

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

2 Replies

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

Answer by Rod-Green · Nov 07, 2011 at 10:28 PM

 Transform targetTransform = null;

 void Update()
 {
     if(targetTransform != null)
         this.transform.LookAt(targetTransform, Vector3.up);
 }

Then all you'd need to do it set the targetTransform value to be the transform you wish to target.. i.e.

   public void SetTarget(Transform p_transform)
   {
         targetTransform = p_transform;
   }



 SetTarget(yourEnemyTarget); // to target

 SetTarget(null); // to clear


To add smoothing:

   void Update()
   {
         if(targetTransform != null)
         {
               Quaternion curRot = this.transform.localRotation;
               Quaternion lookRot = Quaternion.LookRotation(targetTransform.position - this.transform.position, Vector3.up);
               this.transform.localRotation = Quaternion.Lerp(curRot, lookRot, Time.smoothDeltaTime);
         }
   }
Comment
Add comment · Show 7 · 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 Babilinski · Nov 07, 2011 at 10:50 PM 0
Share

this will not let me toggle on and off.

avatar image Rod-Green · Nov 08, 2011 at 01:58 AM 0
Share

I extended it.. does that work?

avatar image Babilinski · Nov 08, 2011 at 07:30 PM 0
Share

Yes. And what if I would like to make it rotate smoothly

avatar image Rod-Green · Nov 08, 2011 at 08:18 PM 0
Share

Smoothing requires a lerp.. I added the method at the bottom of my answer.

avatar image Babilinski · Nov 08, 2011 at 08:29 PM 0
Share

Thank you! Can you help me another question please!

http://answers.unity3d.com/questions/184053/targeting-multiple-enemys.html

Show more comments
avatar image
1

Answer by dannyskim · Nov 07, 2011 at 10:37 PM

Well, there are several ways that you can do this. Since you have no code attached, I'm assuming you haven't tried using LookAt().

In your player's script, simply use:

 transform.LookAt( enemyTransformHere, Vector3.up );

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

Now, this particular method included in the Unity Engine is very limiting, and I'm also going to assume that you are going to want additional parameters, which this method may hinder against. You'd have to provide more detail depending on your specific situations in that case.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Make object face direction only on one axis 1 Answer

move the object where camera look 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 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