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 ragefordragons · Jun 06, 2018 at 03:04 PM · prefabsshootingmousepositionbullets

How to make 2d shooting towards the mouse but have it continue in staright line afterwards?

Title pretty much says it, tight now my code just has the bullet stop at the mouse position, so how would I make it continue past that? I have to codes for shooting, the controll (where the bullet is instantiated) and the bullet code itself which is what handles everything else right now. Ill paste the bullet code here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : MonoBehaviour {
 
     private Vector2 target;
     public float speed;
 
     public float turnSpeed;
 
     void Start()
     {
         target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         StartCoroutine(destroyAfterTime());
        
     }
 
     private void Update()
     {
         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
         Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
 
     }
 
     
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "wall" )
         {
         Destroy(gameObject);
 
         }
     }
 
 
     IEnumerator destroyAfterTime()
     {
         yield return new WaitForSeconds(2f);
         Destroy(gameObject);
     }
 
 }

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 Harinezumi · Jun 06, 2018 at 03:26 PM 0
Share

It is stopping at the mouse position, because you are moving to it, but you should be moving in the direction of the mouse position (where it was when you fired the shot).
In Start(), ins$$anonymous$$d of storing the mouse position as target, store the direction (normalized), and then in Update() use $$anonymous$$oveTowards(transform.position, transform.position + direction * speed, speed * Time.deltaTime). To get the direction, do something like this:

 direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized;

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Kciwsolb · Jun 06, 2018 at 03:27 PM

So, do you just want it to fire in a straight line in the direction of the mouse? If so...

Store this variable:

  Vector3 moveDirection;

Do this in Start:

 moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
 moveDirection.z = 0;       
 moveDirection.Normalize();

Add this in Update:

 void Update()
 {
     transform.position = transform.position + moveDirection * speed* Time.deltaTime;
 }

We first store a Vector3 that will be our direction to the mouse.

We then calculate a direction to the mouse based on our mouse position.

Then we set the z value to zero (since we don't want to move on z since this is 2D).

Then we normalize that Vector3 so it has a magnitude of 1.

Then we update our position being sure to account for speed and frame rate.

Comment
Add comment · Show 6 · 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 ragefordragons · Jun 06, 2018 at 03:48 PM 0
Share

Its giving me an error for the " += " in update

avatar image Harinezumi ragefordragons · Jun 06, 2018 at 03:54 PM 1
Share

That's because Transform.position is a property, not a variable, so you cannot use += operator on it. However, this is easy to fix, you can replace it with transform.position = transform.position + <the rest is the same>;.

avatar image Kciwsolb Harinezumi · Jun 06, 2018 at 04:17 PM 0
Share

Yeah this^ Thanks! This is what happens when I scribble an answer while I'm eating lunch.

Show more comments

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

90 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

Related Questions

Bullet speed changes depending on how close and far I click the mouse button 2 Answers

fps shooting enemy 1 Answer

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

shooting raycast 1 Answer

How to make bullets spread in unity2D? 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