Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Saif_youssef89 · Apr 18, 2019 at 12:18 PM · rotationtransformvector3quaternionangle

Make a side of an object LookAT another object

Basicly I have 2 ships, one is controlled by the player and the other by the computer.

I have a script where the AI Ship rotates towards the player and starts moving towards him. But, once within firing range, it needs to rotate around 90 degrees left or right so that the cannons on either side can have a shot and that is where i've been having difficulty.

I've tried multiple methods but none have worked, seems that i'll never understand Quaternions.

 else if(Vector3.Distance(transform.position, player.position) < firingRange && Vector3.Distance(transform.position, player.position) > retreatDistance)    //If in firing range
 {
     //stop moving
     transform.position = this.transform.position;
 
     //rotate & fire    
     Vector3 fireAngle =  transform.rotation * new Vector3(0,90,0);      //add 90 degrees on Yaxis
     Quaternion rotation = Quaternion.LookRotation(fireAngle);
     transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed + Time.deltaTime);
 }

Any help would be greatly appreciated as I've been stuck in this part for way too long

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

· Add your reply
  • Sort: 
avatar image
1

Answer by ShadyProductions · Apr 18, 2019 at 12:54 PM

Hello,

You can grab the direction of the target, and then set the side of your ai ship to point at the direction.

 var direction = (target.transform.position - aiShip.transform.position).normalized;
 // Set the right side to point at the direction    
 aiShip.right = direction; // You could slerp this to make the transition more smooth instead of instant

You'll have to rotate to left or right based on which side is the fastest to turn towards the target. I assume you could use Dot product for this.

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 Saif_youssef89 · Apr 19, 2019 at 06:21 AM 0
Share

Thank you so much for your help @ShadyProductions!

It worked almost as intended, like you mentioned, I need to Lerp the last statement for a smoother transition.But Quaternions are my weakness and i could never figure them out for life of me (Always sucked at $$anonymous$$ath). I've been trying to get the Lerp working, this is what i got so far:

 Vector3 fireDirection = (player.transform.position - transform.position).normalized;
 Quaternion rotation = Quaternion.LookRotation(firedirection);
 transform.right = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);

But as you can probably tell I get an error, can you please tell me what I'm doing wrong and if you could explain it in more detail for my sake and for everyone else following this question!

$$anonymous$$uch appreciated!

avatar image ShadyProductions Saif_youssef89 · Apr 25, 2019 at 07:56 PM 0
Share

@Saif_youssef89 You can use slerp to spherically interpolate, this treats the vectors as directions rather than points in space like lerp does. This should work:

 var direction = (Target.transform.position - transform.position).normalized;
 transform.right = Vector3.Slerp(transform.right, direction, Time.deltaTime);

Sorry for the late reply.

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

178 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to smoothly rotate to certain directions using input axis 1 Answer

Rotate floor plane in-game via C# script 1 Answer

Smooth Y rotation based on X rotation (C#) 0 Answers

c# modify only one axis of a quaternion 2 Answers

Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 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