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 sonic220 · Oct 03, 2012 at 10:29 PM · mouseshootbullets

Shooting at mouse position

Hi guys I need some help with my code, it's a simple 2d shooter, I want the player to shoot in the direction the mouse is pointing, this is my code:

  if(Input.GetButtonDown("Fire1")){
             var mouse = Input.mousePosition;
             mouse.z = 1.0; //diobonino
             var mouseworldpos : Vector3 = Camera.main.ScreenToWorldPoint(mouse);
             mouseworldpos.y = transform.position.y;
             Debug.Log(mouseworldpos);
             var bullet_dir : Vector3 = mouseworldpos - transform.position;
             bullet_dir = bullet_dir.normalized;
             var clone = Instantiate(bullet1,transform.position + bullet_dir * 2,Quaternion.LookRotation(mouseworldpos, Vector3.forward));
             clone.GetComponent(Rigidbody).AddForce(bullet_dir * bullet_speed);
         }

My first problem is that sometimes it shoots in the direction of the old mouseposition, especially when the player is moving. My secondo problem is the rigidbody, is there another way to shoot bullets that can detect collision without using rigidbody?

Thanks everybody

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
0

Answer by ThePunisher · Oct 04, 2012 at 12:14 AM

Answering second problem here...

If you don't need to simulate bullets in real time then you could simply use RayCasting to get the behavior for a shooter. All you would need in this case would be a collider component (like box/mesh colliders).

Try the following script. Add it to any GameObject, like the main camera. Then create a cube and place it somewhere where your camera can view it. Separate your scene and game views so that you can view both simultaneously and hit play. Left-Click around the game window and you'll see a red line is drawn on the scene view. Any time you click on an object with a collider you'll see a debug message come up with the name of the GameObject you hit. Let me know if this would work for what you want to do and I can help you customize it further.

 using UnityEngine;
 using System.Collections;
 
 public class Shooter : MonoBehaviour
 {
     private Ray ray;
     private RaycastHit hit;
     // Use this for initialization
     void Start ()
     {
     
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, 25.0f))
             {
                 Debug.Log(string.Format("Hit: {0}!", hit.transform.gameObject.name));
             }
             else
             {
                 Debug.Log("Missed!");
             }
         }
 
         Debug.DrawLine(ray.origin, ray.direction * 25.0f, Color.red);
     }
 }
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 sonic220 · Oct 04, 2012 at 09:22 AM 0
Share

I think that simulates the fire of a really fast bullet (I just call a behaviour if the ray interpolate with an object), I need to instantiate a bullet that might react to an hit

avatar image ThePunisher · Oct 04, 2012 at 06:56 PM 0
Share

Hmm, alright. Well then are you sure this is correct "var bullet_dir : Vector3 = mouseworldpos - transform.position;"?

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

10 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

Related Questions

Can't shoot towards mouse click point 1 Answer

Help First Person Shooter Bullets instantianion 2 Answers

The code is giving me errors 1 Answer

Shoot directions 2 Answers

Adding movement speed of Player to 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