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 question was closed Oct 22, 2012 at 06:03 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Sooper1337 · Oct 21, 2012 at 01:38 AM · c#rotationquaternioncoroutineslookrotation

LookRotation Between 2 Points

Basically I am trying to get an object to look at one object or a specific rotation, then smoothly switch to another rotation or object, and to continuously loop.

I have written a small script that i think is on the right track for working using Co-routines.

     public Transform pointB;
     public Transform pointC;
     
     public IEnumerator TurretLook (Transform thisTransform, Quaternion startAngle, Quaternion endAngle, float time)
     {
         float i = 0.0f;
         float rate = 1.0f / time;
         while (i < 1.0) {
             i += Time.deltaTime * rate;
             thisTransform.rotation = Quaternion.Lerp (startAngle, endAngle, i);
             yield return new WaitForSeconds(0);
         }
     }
     
     IEnumerator Start ()
     {
         Quaternion pointA = transform.rotation;
         while (true) {
             yield return TurretLook(transform, pointA, Quaternion.LookRotation (pointB.position - transform.position), 3.0f);
             yield return TurretLook(transform, pointA, Quaternion.LookRotation (pointC.position - transform.position), 3.0f);
         }
     }
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Sooper1337 · Oct 21, 2012 at 10:19 PM

Ended up Rewriting the Script. Posting Here in case anyone else might want this solution as well.

Uses 2 Empty Gameobjects with colliders attached to detect when the ray-cast intersects with one, then switched states to target the opposite.

 using UnityEngine;
 using System.Collections;
 
 public class MainMenuTurret : MonoBehaviour 
 {
     private float aimSpeed;
     public float dampening;
     public Transform[] points;
     
     public enum Target{
         point1,
         point2
     };
     
     public Target tState;
     
     public void MenuIdle ()
     {
         Ray idle = new Ray (transform.position, transform.forward);
         RaycastHit hit;
         
         Debug.DrawRay(idle.origin, idle.direction * 5, Color.blue);
         
         if (Physics.Raycast (idle, out hit, 5)) {
             GameObject hitObj = hit.collider.gameObject;
             
             if (hitObj.tag == "P1")
                 tState = Target.point2;
             if (hitObj.tag == "P2")
                 tState = Target.point1;
         }
     }
     
     void Update ()
     {
         aimSpeed = dampening * Time.deltaTime;
         
         MenuIdle ();
         
         switch (tState) {
         case Target.point1:
             transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation (points [0].position - transform.position), aimSpeed);
             break;
             
         case Target.point2:
             transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation (points [1].position - transform.position), aimSpeed);
             break;
             
         }
     }
 }
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

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

Flip over an object (smooth transition) 3 Answers

Quaternion.Slerp with Quaternion.LookRotation causes unexpected results 1 Answer

i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers

Quaternion.LookRotation and Vector3.SmoothDamp Problems 1 Answer

Rotate object without storing facing 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