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 Perrymanon · Apr 26, 2014 at 07:14 PM · quaternionrotateorbitquaternion.slerp

How do I make an object face forward while it is orbiting another object?

I have a simple enemy in my game which orbits the player if it comes within a certain distance of them, the enemy orbits correctly and everything else carries on correctly. But I cannot figure out how to make the enemy appear that it is facing forward, so the enemy rotates facing the player and looking very strange.

So how can I make the enemy face the way it is moving while orbiting?

Here is the movement section of my code

 using UnityEngine;
 using System.Collections;
 
 public class MineLayerAI : MonoBehaviour {
 
     public GameObject target;
     public Transform Target;
     public float rotationSpeed = 3.5F;
     public float moveSpeed = 3.5F;
     public Transform enemyTransform;
     public Rigidbody MinePrefab;
     public float WaitLayTime = 2.0f;
     public bool LayTime = false;
     public float OrbitSpeed = 50.0f;
     public bool wallHit = true;
     
     
     void Awake(){
         
         enemyTransform = transform;
         
         if(!target)
             {
                 target = GameObject.FindWithTag("Player");
             }
         if(!Target)
             {
                 Target = GameObject.FindWithTag("Player").transform;
             }
         
     }
     
     void Update () 
     {
         
         
         float currentDistance = Vector3.Distance(transform.position, Target.position);
         
         if(currentDistance > 2.5)
             {
                 //rotate towards player
                 enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
                 //move towards player
                 enemyTransform.position += enemyTransform.forward * moveSpeed * Time.deltaTime;
             }
         else
             {
                 if(wallHit == true)
                 {
                     //rotate towards player
                     enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
                     //orbits around the player
                     transform.RotateAround (Target.position, Vector3.up, OrbitSpeed * Time.deltaTime);    
                 }
                 if(wallHit == false)
                 {
                     //rotate towards player
                     enemyTransform.rotation = Quaternion.Slerp(enemyTransform.rotation, Quaternion.LookRotation(target.transform.position - enemyTransform.position), rotationSpeed*Time.deltaTime);
                     //orbits around the player
                     transform.RotateAround (Target.position, Vector3.down, OrbitSpeed * Time.deltaTime);
                 }
                  
             }

Thank you in advance for any help

Jack Perry

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 LostInTime · Apr 26, 2014 at 07:22 PM 0
Share

U could use Transform.LookAt(). Then u specify a vector

avatar image Perrymanon · Apr 26, 2014 at 08:49 PM 0
Share

Im not sure how I would get the vector though, as it will be constantly changing.

1 Reply

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

Answer by robertbu · Apr 26, 2014 at 07:41 PM

I'm having trouble sorting out the specific of the here to write you the exact line of code you need. A general solution is to use the cross product of a vector between the center and the orbiting object and the axis of rotation. So if the axis of rotation is the 'Y' axis, then the code might look like this (from a script on the orbiting object):

 var lookDir = Vector3.Cross(transform.position - center.position, Vector3.up);
 transform.rotation = Quaternion.LookRotation(lookDir, Vector3.up);

 
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 Perrymanon · Apr 26, 2014 at 08:58 PM 0
Share

Edit: I have realised that I just needed to swap the centre.position and the transform position around.

Hey, so that does work for the rotation, the only thing is it seems to be rotating in reverse.

Why would it be doing that?

avatar image Perrymanon · Apr 26, 2014 at 09:01 PM 0
Share

Apologies for more questions, but is there any way to make the transition from movement to rotation smooth?

avatar image robertbu · Apr 26, 2014 at 09:05 PM 0
Share

If your object is facing 180 degrees the wrong way, either 1) swap the order of the two parameters in the Vector3.Cross or 2) netage the axis in the Vector3.Cross(), or 3) negate the result of the Vector3.Cross(). All three should flip the look direction 180 degrees. I don't have an answer for your smooth transition issue.

avatar image Perrymanon · Apr 26, 2014 at 09:07 PM 0
Share

Thank you very much for your help, I'll see what else I can dig up on smoothing the transition. Thanks again, I've been banging my head about that for days.

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

21 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

Related Questions

Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers

Rotating a boat on two axes with force. 2 Answers

Third Person Camera Orbit 0 Answers

Rotate an object relative to Camera 2 Answers

Transform rotation and position on key input: rotation only working the first time 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