Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Xysch · Nov 02, 2017 at 11:04 PM · instantiateprefabpositioneuler

Instantiate bullet toward object

Hi, I'm currently working on a dog fighting game and I would like that when the bullet hits the shield, it ricochet and flies towards the player with increased. It is a 2 person game so whenever the bullet of one player hits the shield of another, it deletes itself and creates the other player's bullet.

I currently have it where the bullet collides and makes a 180, however I am unable to get it to go towards the player. Does anyone know how to solve this?

 //Turns around normally
 Instantiate(bulletPrefaby, transform.position, Quaternion.Euler(0,0,180)); 
 bulletPrefaby.GetComponent<Bullet>().damage = damage + increaseDamage;
 Destroy(gameObject);
 
 //Instantiates with incorrect angle
 float playerPosition = playery.transform.position.z - transform.position.z;
 Instantiate(bulletPrefaby, transform.position, Quaternion.Euler(0,0, playerPosition)); 
 bulletPrefaby.GetComponent<Bullet>().damage = damage + increaseDamage;
 Destroy(gameObject);

Thank you!

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
0

Answer by Glurth · Nov 02, 2017 at 11:40 PM

Quaternion.Euler takes ANGLES as the parameters, not a positional offset.

Try using this one instead: https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

The below is untested & uncompiled, but should give you a good sample:

 Vector3 offset = playery.transform.position - transform.position;
 Quaternion facingPlayerX = Quaternion.LookRotatation(offset,Vector3.up);
 Instantiate(bulletPrefaby, transform.position, facingPlayerX);
Comment
Add comment · Show 7 · 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 Glurth · Nov 02, 2017 at 11:46 PM 0
Share

oops: posted wrong version of function: fixed now.

avatar image Xysch Glurth · Nov 02, 2017 at 11:55 PM 0
Share

This does make a new bullet, however they don't seem to work like they do with Quaternion.Euler. For some reason they don't move and create a smaller version of the prefab although it is the same exact prefab as the normal bullets. They don't create a collider either and I get an error saying the collider "did not create any as they all failed verification. This could be because they were deemed too small or the vertices were too close". Any ideas?

avatar image Glurth Xysch · Nov 03, 2017 at 02:15 AM 0
Share

too close: this could be a problem- passing a vector that is == to Vector3.zero, into the LookRotation could give you problems. But that would mean playery.transform.position == transform.position: the bullet and player are in the same place- automatic hit?

Other than that, the ONLY thing that should have changed is the rotation you are passing to the Instantiate function.

Show more comments
avatar image
0

Answer by Xysch · Nov 10, 2017 at 08:52 AM

I did not end up finding a solution but I instead chose to change the rotation after the bullet was created.

 public bool reflectedBullet = false;
 
 void Start()
 {
     //If bullet clone is reflected
     if(reflectedBullet == true)
     {
         Vector3 FacePlayer = player1.transform.position - transform.position;
         float angle = Mathf.Atan2(FacePlayer.y, FacePlayer.x) * Mathf.Rad2Deg;
         Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
         transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 1000000);
     }
 }
 
 private void OnTriggerExit2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "SheildPlayer2" && player1Bullets == true && playerMissile == false)
     {
         //Create transform in order to only edit the clone and not prefrab
         Transform reflectedBullet = Instantiate(player2BulletPrefab, transform.position, transform.rotation * Quaternion.Euler(0, 0, 180));
         reflectedBullet.GetComponent<Bullet>().damage = damage + increaseDamage;
         reflectedBullet.GetComponent<Bullet>().reflectedBullet = true;
         Destroy(gameObject);
     }
 }
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

113 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

Related Questions

Why are the children of the prefab I am instantiating getting the same exact transforms of its parent? 0 Answers

Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer

Collider.bounds position is wrong? 3 Answers

Need Help With 2D Array Grid Position Tracking With JavaScript 1 Answer

GameObject change Position after game started 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