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 timo0060 · May 15, 2012 at 12:53 AM · 2djavascriptmouseshootingsidescroller

Sidescroller Bullets Follow Mouse After Being Shot

So after I instantiate my bullets and fire towards my mouse in this sidescroller, the bullets continue to follow the mouse. If you move the mouse left, the bullets will curve left. The speed of the bullets will also be slower if you shoot close to the player, because the bullet wants to stay around the mouse. Here's a short video so you can visualize my problem: http://youtu.be/tT09CA-pY9M .

Now the code on my PLayerScript (To find location of the mouse and instantiate the bullet)

 // this creates a horizontal plane passing through this object's center
 var plane = Plane(transform.position, Vector3.up);
 // create a ray from the mousePosition
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 // plane.Raycast returns the distance from the ray start to the hit point
 var distance: float;
 if (plane.Raycast(ray, distance)){
     // some point of the plane was hit - get its coordinates
     hitPoint = ray.GetPoint(distance);
     hitPoint.z = -19;
     }
     
  

direction = hitPoint - transform.position;

 if(Input.GetMouseButtonUp (0) )
 {
     var instantiatedbullet = Instantiate(bullet,GameObject.Find("SpawnPoint").transform.position, Quaternion.Euler(direction.normalized)); 
 }

Then my code on my BulletScript to move it forwards after being instantiated.

 //amount to move bullet
 amtToMove = bulletSpeed * Time.deltaTime ;
 
 transform.Translate(Vector3.fwd * amtToMove);

So how do I have the bullet remember the spot the cursor was at when the mouse button was clicked up, and only move to and then past that point?

Comment
Add comment · Show 4
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 Pandiux · May 15, 2012 at 01:42 AM 0
Share

Try something like this,

function Update(){ mouseX = Input.GetAxis("$$anonymous$$ouseX")l if (mouseX){ bulletTransform.Rotate(mouseX Time.deltaTime sensitivityVariable, 0, 0); } }

avatar image timo0060 · May 15, 2012 at 01:56 AM 0
Share

What do you mean by sensitivity variable?

avatar image Gennadios · May 15, 2012 at 01:59 AM 0
Share

Hey mate, I posted an incomplete answer earlier but after thinking on it a bit more, your problem is:

var instantiatedbullet = Instantiate(... Quaternion.Euler(direction.normalized));

You're tying the bullet's rotation to a global variable that gets updated with each frame, so it's rotation will be wherever the mouse is pointing at the time and it'll transform.Translate(Vector3.fwd * amtTo$$anonymous$$ove) towards that rotation.

since hitPoint isn't prefixed by a var in your script I still think it's global, try instantiating it within the function you're using to make it local and all should be fine.

Can't wait the hour it takes for the actual answer to finally be visible so that I can edit it.

avatar image timo0060 · May 15, 2012 at 02:58 PM 0
Share

I use hitPoint as a global variable so my spawn point will look at it while rotating around an empty game object. $$anonymous$$y firend who programs in C# but not in unity quickly wrote up a code that would help me find the angle between the player and the mouse and just shoot along that angle. The code is this (part Pseudo code).

mPos = vector3 (mouse position) pPos = vector3 (player position) bPos = vector3 (Bullet position)

xdir = mPos.x - pPos.x ydir = mPos.y - pPos.y

angle = Atan2(xdir,ydir)

lx = cos(angle) ly = sin(angle)

bPos.x += bullet$$anonymous$$oveSpeed lx Time.deltaTime bPos.y += bullet$$anonymous$$oveSpeed ly Time.deltaTime

So my question now is, would that work properly on a 2d sidescroller, and how would it look if it was converted to usuable javascript? Thanks for any help, and thank you for your previous help as well.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Gennadios · May 15, 2012 at 02:21 AM

I can't help overhaul your code because I'm not particularly with Javascript, but is the

  if (plane.Raycast(ray, distance)){
     // some point of the plane was hit - get its coordinates
     hitPoint = ray.GetPoint(distance);
     hitPoint.z = -19;
     }

block in the Update function, and is hitPoint global? If both are true it looks like a new hitPoint is generated every update based on wherever your mouse is at the time. Trying firing multiple bullets at once and see if they all follow the mouse.

Try passing a copy of the hitPoint to the bullet of using a global.

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 Pandiux · May 15, 2012 at 02:20 AM

Try something like this,

function Update(){ mouseX = Input.GetAxis("MouseX")l if (mouseX){ bulletTransform.Rotate(mouseX Time.deltaTime sensitivityVariable, 0, 0); } }

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullets follow mouse after shooting in Sidescroller 1 Answer

Aiming with the mouse in a 2d shooter, and then shooting to the mouse 2 Answers

Code Converstion into javascript Help? 2 Answers

Shooting at mouse position in 2d from a Child Object 0 Answers

2d camera help! 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