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 stockhouse50 · Apr 11, 2015 at 10:07 AM · 2dinstantiatecursor

Instantiating a bullet object from a 2D object that looks at mouse cursor

Hi, I have a 2D object that continuously looks at the mouse cursor. I am using an overheard view. Here is the code that I used for this:

 Vector3 screen_pos = Camera.main.ScreenToWorldPoint (new Vector3 (mouse_pos.x, mouse_pos.y, 0));
 transform.LookAt ( screen_pos, Vector3.forward);

Before I changed to using the mouse cursor I was just using left/right arrow keys to rotate the object and I was able to instantiate bullet objects using this code:

 Instantiate (BulletPrefab, transform.localPosition+(transform.up *.5f)+(transform.right*-.3f) , transform.localRotation);  // bullet instantiates from front right

The above code worked fine before but now with the mouse cursor code added in, the bullets move in random directions and sometimes instantiate in the wrong position.

Thanks.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by equus_ligneus · Apr 11, 2015 at 12:01 PM

You are currently placing your bullet with hard-coded offset. Try this instead:

 // get the vector from current pos to mouse (you have to cache your screen_pos)
 Vector3 direction = (screen_pos - transform.position).normalized;
 
 // Instantiate your bullet with 0.5 units offset to your position and your rotation
 Instantiate(BulletPrefab, transform.position + direction * 0.5f, transform.rotation);


This code will crash at .normalized if your cursor's position equals your 2D-object's position because normalize will divide by the length of (screen_pos - transform.position) which, in that case, is zero.

To circumvent annoying bugs (bullet flies at camera...) with this system make sure to have the z-value of both screen_pos and transform.position aligned:

 Vector3 screen_pos = Camera.main.ScreenToWorldPoint (new Vector3 (mouse_pos.x, mouse_pos.y, 0));
 screen_pos.z = transform.position.z;

 
Comment
Add comment · Show 4 · 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 stockhouse50 · Apr 12, 2015 at 10:45 PM 0
Share

Ok, so it appears the bullet will start from the correct position but the bullet will still go in random directions. Now in the bullet class I have the code.

rigidbody2D.AddForce ( transform.up * speed);

Do I need to get the direction from the spaceship object (object shooting the bullet) and use that in the AddForce method of the bullet class in place of transform.up ?

Also, when I use the line screen_pos.z = transform.position.z; for some reason the spaceship object disappears.

avatar image equus_ligneus · Apr 13, 2015 at 07:40 AM 0
Share

Yes, you need to use the direction from your spaceship. $$anonymous$$aybe create a method like this in your Bullet script:

 public void SetDirection(Vector3 _direction)
 {
      GetComponent<Rigidbody2D>().velocity = _direction * speed;
 }

(If you want to set the speed, do it directly :)) Then call it from your spaceship after initialization:

 // ... 
 
 // Instantiate your bullet with 0.5 units offset to your position and your rotation
 GameObject go = Instantiate(BulletPrefab, transform.position + direction * 0.5f - Vector3.forward * 0.1f, transform.rotation) as GameObject;

 // Assu$$anonymous$$g the bullet class is Bullet
 go.GetComponent<Bullet>().SetDirection(direction);

As for your problem with the disappearing bullets: $$anonymous$$aybe they were created behind your spaceship. I added an offset of 0.1 units towards the camera in the instantiate part. It should fix the problem if z-order was the issue.

avatar image stockhouse50 · Apr 14, 2015 at 07:01 PM 0
Share

Ok, so now the bullets shoot in the correct direction. However, the bullets are not moving with the front of the bullet towards the direction they are moving (sometimes they are diagonal, backwards, forward depending on direction).

Also, the spaceship is still invisible. I also noticed that when the spaceship is hidden I cannot move the spaceship up/down/sideways with the keys like I can when the spaceship is visible.

avatar image equus_ligneus · Apr 14, 2015 at 09:17 PM 0
Share

About the space ship: The problems lies at your LookAt call... For performance reasons backsides (or insides) of objects do not get rendered. By calling LookAt and setting Vector3.forward as the normal vector of the plane that is your spaceship, you turn the plane so that it faces away from the camera, thus the camera can't see it. Use -Vector3.forward as second parameter in your LookAt call.

As for your bullet rotation: I can't say what happens without you providing footage, especially with the rotation of your space ship being messed up.

avatar image
0

Answer by Tomer-Barkan · Apr 14, 2015 at 07:59 PM

You're using transform.position + transform.up. transform.up is relative to the object, so when you rotate it with LookAt, the up direction changes as well. If you want it to be always on top of the object, regardless of where the object is facing, you can use Vector3.up instead.

If this is not what you want, you better explain yourself better, and a screenshot or schematic will help.

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

Answer by stockhouse50 · Apr 18, 2015 at 08:35 PM

Ok, I am willing to start from scratch on this rotation idea so if that means using an alternative to lookAt() I'm fine with that. I attached a schematic and I will try to explain it in further detail. The game is 2D with an overhead/birds eye view of the spaceship. Basically wherever the mouse cursor moves on the screen, I want the spaceship to rotate only around the z-axis ( basically if a rod came out from the screen, it would rotate around that as shown in the schematic.) and have the front facing towards the mouse cursor. alt text

So in the schematic pretend the arrow is the cursor and the front is always facing the cursor. The green triangles in the schematic are the bullets. When the bullets are instantiated, the front of the bullet is in the same direction as the spaceship.

If I explained anything poorly or used wrong terminology please correct me because I am trying to learn the lingo along the way. Thanks.


spaceship-rotation.jpg (62.1 kB)
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
avatar image
0

Answer by stockhouse50 · Apr 19, 2015 at 12:31 PM

Alright finally after all my searching I found the answer. The solution I found doesn't use lookat() but does the job.

 Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
          diff.Normalize();
  
          float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter, called on both instantiated objects 2 Answers

A null value was found where an object instance was required. 1 Answer

Make object follow mouse cursor in 2D 1 Answer

prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers

Instantiated object won't move! 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