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 VladN · Jan 04, 2012 at 07:15 PM · transformanglerelative

Angle of object relative to transform of second object

Hi gang!

Working on a ship game, could really appreciate a pointer on this.

Trying to find the angle of object B (image below) in relation to object A, regardless of object A's rotation. Essentially, a side-wise LookAt reading.

In effect, finding Ship B's angle in relation to Ship A's position is so it can turn it's closes left or right side (and rain fiery justice).

Tried:

  • RayCast - great for firing, but not to anticipate the positioning beforehand.

  • Quaternion.Angle - perfect, except it is considering the other object's rotation.

  • Vector3.Angle - doesn't consider the individual rotations, only positions.

Any pointers would be much appreciated, thanks in advance!

alt text

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
2
Best Answer

Answer by mpavlinsky · Jan 04, 2012 at 07:52 PM

So if I understand correctly you want Ship B to face it's side towards Ship A. You can achieve this with a little linear algebra to find the at vector you would like and then just use Quaternion.SetLookRotation(). I made a brief script to demonstrate:

 using UnityEngine;
 using System.Collections;

 public class ShipTest : MonoBehaviour {
 
 // Set me to the object you want however you see fit.
 public GameObject targetObject = null;
 
 // Update is called once per frame
 void FixedUpdate () {
     // Subtract the positions to find the vector from our object to the target.
     Vector3 toTarget = targetObject.transform.position - transform.position;
     toTarget.Normalize();
     
     // Find the vector perpendicular to the 'up' and 'right' (in this case the vector to the target object) vector to find the new 'at'.
     Vector3 newAt = Vector3.Cross(transform.up, toTarget);
     
     // Make new rotation.
     Quaternion newRotation = new Quaternion();
     newRotation.SetLookRotation(newAt);
     transform.rotation = newRotation;
 }
 }
Comment
Add comment · Show 1 · 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 VladN · Jan 05, 2012 at 02:53 AM 0
Share

Thanks mpavlinsky!

I used your method and it worked, did one for the other side, but the challenge was detecting which side was closer. SetLookRotation was also a bit too precise for my liking. I ended up using a different code I'll post below, but yours got me in the right direction; much appreciated!

avatar image
0

Answer by VladN · Jan 05, 2012 at 03:34 AM

In the end used the 360 angle trick mentioned here along with some of the suggestions above:

     //subtract positions to find the vector to target

   var toTarget : Vector3 = target.transform.position - transform.position;

   //360 angle from object (runs ContAngle function which in turn runs AngleDir

 var attackAngle = ContAngle(transform.forward, toTarget, transform.up);

 //distance to target

 var shipDistance = Vector3.Distance(transform.position, target.transform.position);

 

 //if ship is within range (I use 8 for testing)

 if(shipDistance <= 8){

    //turn left/right side, depending on which one is closer

     if(attackAngle >=0 && attackAngle <90 || attackAngle >=180 && attackAngle <270){

         transform.Rotate(0, -1*Time.deltaTime*rotationSpeed,0);

     }

     if(attackAngle >90 && attackAngle <180 || attackAngle <=360){

         transform.Rotate(0, 1*Time.deltaTime*rotationSpeed,0);

     }

 }

 //if ship is not in range

 if(shipDistance >8){

 //slowly rotate to face/chase target 

     neededRotation = Quaternion.LookRotation(target.transform.position - transform.position);

     transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, Time.deltaTime*rotationSpeed/35);

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need to know euler angles of Vector3 direction relative to other Vector3 direction 1 Answer

Specifying a game object by height or width or length instead of localScale 1 Answer

Finding Face Angle 0 Answers

Parent transform 3 Answers

Doom-like Sprite Models 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