Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 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 VewixxPlayer · Feb 14 at 09:51 PM · transformquaternioninstantiate prefabinstantiationquaternion.lookrotation

"Quaternion.LookRotation" not working when Instantiate

I want to make an enemy that is capable of shooting projectiles at the player. For this, I am using 2 C# scripts. One that controls the enemy's AI and one that controls the projectile.


The projectile script simply gets the projectile data (such as the speed, damage, and a bool indicating if a player or an NPC shot it) and applies a velocity to its own Rigidbody using the aforementioned values. It also manages the collision system to detect if it hit a player, an enemy, or the terrain (even tho this is not relevant to the question).


The AI script controls a variety of options but the only one we are interested in is the projectile shooting function, which passes the projectile data values and instantiates a copy of the projectile prefab (which contains the projectile script).


The problem is that when it instantiates the projectile, the Quaternion.LookRotation (using it for the projectile to get shot in the direction we want it to) only takes the Y value of the objective's GameObject transform. I have tried to manually insert random values in the Quaternion Vector3 and it seems to work fine, but will not shoot the projectile in the direction of the GameObject when trying to use its position. I have added a print statement to make sure the script knows where the GameObject is, and it does. I will attach a video with the play result and the 2 scripts.


This is the video


Here is the part of the Projectile Script we are interested in:

 private Rigidbody projectileRigidbody;
 
 private float projectileSpeed;
 private float projectileDamage;
 private bool isPlayerShot;
 
 private void Awake(){
     projectileRigidbody = GetComponent<Rigidbody>();
 }
 
 private void Start(){
     projectileRigidbody.velocity = transform.forward * projectileSpeed;
 }
 
 public void SendProjectileData(float newProjectileSpeed, float newProjectileDamage, bool newIsPlayerShot){
     projectileSpeed = newProjectileSpeed;
     projectileDamage = newProjectileDamage;
     isPlayerShot = newIsPlayerShot;
 }

And here is the Basic AI script:

     [SerializeField] private bool canShoot;
     [SerializeField] private float projectileDamage;
     [SerializeField] private float projectileSpeed;
     [SerializeField] private float shootCooldown;
 
     private Transform shootPoint;
     [SerializeField] private Transform shootObjective;
 
     [SerializeField] private Transform projectilePrefab;
     private ProjectileScript projectileScript;
 
 // Awake function
 
 private void Awake(){
         if(canShoot == true){
             shootPoint = gameObject.transform.Find("ShootPoint").transform;
             projectileScript = projectilePrefab.GetComponent<ProjectileScript>();
         }
     }
 
 // Actual Shoot function
 
 private void ShootProjectile(){
         if(canShoot == true){
             projectileScript.SendProjectileData(projectileSpeed, projectileDamage, false);
             print("The ShootObjective position is: " + shootObjective.position);
             
             Instantiate(projectilePrefab, shootPoint.position, Quaternion.LookRotation(shootObjective.position, Vector3.up));
             StartCoroutine(ShootWaitCooldown());
         }
     }
 
     // Cooldown
 
     IEnumerator ShootWaitCooldown(){
         yield return new WaitForSeconds(shootCooldown);
         ShootProjectile();
     }


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

Answer by Captain_Pineapple · Feb 14 at 11:14 PM

Hey there,

welcome to the forum and thank you for posting a detailed and comprehensive question. Glad to put in some effort to help you here. (I really mean it)


If you read the documentation on Quaternion.LookRotation (can be found here) you will see the following line:

  Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);

where it says relativePos.

so your instantiation line most likely just has to be changed to be:

   Instantiate(projectilePrefab, shootPoint.position, Quaternion.LookRotation(shootObjective.position- shootPoint.position, Vector3.up));




Apart from that some advice for your script:

you have a coroutine that spawns coroutines each time it terminates. It is better style if you do it like this:

 IEnumerator ShootRoutine(){
 var cooldown = new WaitForSeconds(shootCooldown);
 while(true) {
          yield return cooldown ;
          ShootProjectile(); // remove the cooldown call from this "ShootProjectile" call
      }
 }

This coroutine above only has to be started once and it then runs infinetly. This is better in that regard that you do not have garbage being created each time the yield return call comes along. Downside to this method is that you cannot change the cooldown as long as the coroutine is running. (you'd have to go back to yield return new WaitForSeconds(shootCooldown) to make that work)


Next thing is that you get the reference for projectileScript in your start function. There you get the reference to the script instance on the prefab. When you change the projectile speed on this instance this will most probably also change the speed of all projectiles which are already in the scene.


Last thing: In case you plan on adding a lot of projectiles you should check out object pooling. This is a method to recycle short-lived objects like projectiles since the process of instantiation and destruction of objects is really bad for performance.


Let me know if that helped and good luck :)

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 VewixxPlayer · Feb 15 at 03:38 PM 0
Share

It really worked! Thank you, it was really helpful, been struggling for a while. Thanks for your answer. Also, I don't plan to modify the cooldown in-game so I changed the coroutine method as well. I will look into the object pooling concept as well, thanks for your help.

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

175 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

Related Questions

Make object lay flat on a sphere + look up? 0 Answers

Quaternion.LookRotation() doesn't look down 2 Answers

Quaternion.LookRotation problem - always rounded to 90 degree angles 1 Answer

Turret rotation with a pivot? 1 Answer

LookRotation with correct roll angle (using angle axis) 0 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