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 CrucibleLab · Mar 20, 2015 at 04:10 PM · projectilequaternion.lookrotationquaternion.slerp

Rotating gameObject to aim at target using Quaternion

alt text

As shown in the above image, I have a Scene where a player and an enemy face each other with a goal to hurt one another using the gun.

Here's what actually happens in the scene: Player manually aims the gun at the enemy using two sliders; one controls the Y-axis rotation (rotate left or right) and the other controls the X-axis rotation (rotate up or down). I intentionally made the velocity of the projectile to be low in order to force the player to aim the gun barrel at an angle -- and this results in a parabolic motion of the projectile.

Once the player's projectile is consumed (i.e., hits something/anything), it is the enemy's turn to counterattack. I set a boolean variable to 'true' to let the enemy's script know that it's time for the enemy's gun to fire:

 canReact = true;

And the enemy's script is as follows:

 ...
 private bool canReact = false;
 private bool shotFired = false;
 private GameObject gunBase;
 private GameObject gunMount;
 private GameObject target;   // target is the player
 private Vector3 gunBaseRelativePos;
 private Vector3 gunMountRelativePos;
 private Quaternion gunBaseRotation;
 private Quaternion gunMountRotation;
 
 void FixedUpdate() {
     if (canReact == true) {
         if (shotFired == false) {
             gunBase = GameObject.FindGameObjectWithTag("enemy_gun_base");
             gunMount = GameObject.FindGameObjectWithTag("enemy_gun_mount");
             target = GameObject.Find("Player");
 
             gunBaseRelativePos = target.transform.position - gunBase.transform.position;
             gunMountRelativePos = target.transform.position - gunMount.transform.position;
 
             baseRotation = Quaternion.LookRotation(gunBaseRelativePos);
             mountRotation = Quaternion.LookRotation(gunMountRelativePos);
 
             gunBase.transform.rotation = Quaternion.Slerp(gunBase.transform.rotation, baseRotation, Time.deltaTime);
             gunMount.transform.rotation = Quaternion.Slerp(gunMount.transform.rotation, mountRotation, Time.deltaTime);
 
             // Notice here that I'm only instantiating the projectile, not firing. 
             // I'm only concerned about getting the Gun's rotation right first.
             clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
 
             // using the 'shotFired' flag to prevent the enemy's gun from shooting continuously
             shotFired = true;
         }
         // below code tells the camera to look at & follow the projectile while it is present in the scene
         if (clone != null) {
             camera.transform.LookAt(clone.transform);
             camera.transform.position = clone.transform.position - distance;
         } else {
             // projectile has been destroyed, so reset the flag
             shotFired = false;
         }
     }
 }
         

Basically, what I'm trying to accomplish using the above code is to have the enemy's gun take an aim at the player -- and this movement would involve two steps: 1) rotate the 'gun base' around Y-axis, and 2) rotate the 'gun mount' around X-axis for the firing angle.

When I run the scene, the 'gun base' rotates slightly to the LEFT (it should actually rotate to the RIGHT) and the 'gun mount' (which controls the angle) doesn't move at all.

So, the obvious questions are:

1) How do I make the 'gun base' rotate correctly so that it faces the direction of the player?

2) How do I make the 'gun mount' to rotate so that it actually fires its projectile at an angle given a velocity of the projectile? When I test only the firing process, the projectiles shoots in a straight line like laser beam.

ground.png (7.5 kB)
Comment
Add comment · Show 1
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 abi-kr01 · Jun 03, 2015 at 10:40 AM 0
Share

you can use look at

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Malek-Bakeer · Jun 03, 2015 at 03:04 PM

it really depends on your model ... its very complicated u need to rotate and multiply by a number and minus 90 degrees (that's what i figured out and its very accurate)

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

23 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

Related Questions

How to make character rotate in the direction of movement and direction of the camera? 1 Answer

Quaternion.Slerp() behaves weirdly when turning to look at moving object 2 Answers

Use curve effect on rotation? 1 Answer

Making Player Rotation Smoother 0 Answers

Help with tower defense game using LookRotation 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