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 06:23 PM · javascriptshootingsidescroller

Code Converstion into javascript Help?

Hello everyone. My friend is helping me make a sidescrolling 2d shooter that runs off of the y and x axis. He doesn't use unity so he wrote some script down and I was wondering if someone could help me convert it to unity javascript and also tell me if it would work for shooting to the mouse. Here is his 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 += bulletMoveSpeed lx Time.deltaTime; bPos.y += bulletMoveSpeed ly Time.deltaTime;

I hope this code will work because shooting in 2d has given me countless problems.

Comment
Add comment · Show 3
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 timo0060 · May 15, 2012 at 07:50 PM 0
Share
  1. That edit you does nothing. Next time code you please help me out more? Shooting is stressing me out, yet it's so simple of a concept.

  2. I got it working, had to put $$anonymous$$athf. in front of Atan2, sin, and cos. But the bullets don't move at all, why won't they move?

avatar image aldonaletto · May 15, 2012 at 09:20 PM 1
Share

@timo0060, this edition was not intended to solve your problem: it just formatted your code. And it helped you - many guys here just refuse to read badly formatted questions.
Anyway, the code above or any variations on the same theme will not work in Unity: it's a full 3D engine, even when powering 2D games. You must think in terms of 3D coordinates, not in 2D screen space.

avatar image Lo0NuhtiK · May 15, 2012 at 09:56 PM 0
Share

He could have just deleted your entire post ins$$anonymous$$d of using up a $$anonymous$$ute or two of his lifespan to format a code the writer of which wasn't concerned with the presentation of and didn't care whether or not it was hard to read etc for the people said code writer was asking help from.

2 Replies

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

Answer by Berenger · May 15, 2012 at 07:52 PM

You need to put all that inside a function called Update(). Here is some things you'll need. Input.mousePosition, Camera.ScreenPointTo, Transform.position, Mathf.Atan.

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 timo0060 · May 15, 2012 at 08:00 PM 0
Share

Thank you. That helps a lot more.

avatar image timo0060 · May 15, 2012 at 10:48 PM 0
Share

I'm trying to get the position of my player (pPos), but I can't get it. I've tried setting up the player as a rigidbody in my script and taking the player.x from it, but I can't add my player as a rigidbody. Any other suggestions on how to get the x and y position of my player?

avatar image Berenger · May 16, 2012 at 02:57 PM 0
Share

To get the player's position, you a reference of its gameobject or of any of it's component to access its Transform, where the position is stored. The whole matrix actually.

Anyway, it depends on what you script is attached to. If it's the player, you can have the position with transform.position. If not, you can either declare the var outside the functions then setting it up in the inspector, or you can use Find functions, preferable FindWithTag (the fastest)

avatar image timo0060 · May 16, 2012 at 03:18 PM 0
Share

Thank you. I decided to just use static variables in my player script to get the x and y positions and then just call them in my Bullet script. $$anonymous$$y only problem now is the bullet won't move. I modified the code to look like this:

transform.Translate.x(bullet$$anonymous$$oveSpeed lx Time.deltaTime); transform.Translate.y(bullet$$anonymous$$oveSpeed ly Time.deltaTime);

Any ideas on how to make the bullets move? They just sit there after being spawned. Thanks for your help so far. This community is a good one.

avatar image Berenger · May 16, 2012 at 03:39 PM 1
Share

Translate is a function, it doesn't have members. You can't call Translate.x(...). Ins$$anonymous$$d, call transform.Translate( Vector3( xstuff, ystuff, 0 ) ). If it's C#, add new before Vector3.

And make sure none of those variable is 0, that the bullet's position isn't modified elsewhere and that the function is actually called. It should be in Update().

Show more comments
avatar image
0

Answer by Scribe · May 16, 2012 at 04:16 PM

This is not a translation of you code but it is code that should work... I am rather bored and should be revising so this was a more entertaining alternative. Hopefully it works though I haven't tested it and as I don't know how your game is set up I don't know if it will be relevant but here you go:

 var GunObj : Transform; //the gun
 var MouseWorldPos : Vector3; //a variable to store position of cursor
 var Bullet : GameObject; //bullet/projectile to be fired
 var ROF : float = 0.5; //Rate Of Fire of your gun
 
 function Update() {
     MouseWorldPos = camera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, GunObj.position.z));
     
     GunObj.LookAt(MouseWorldPos);
     
     if(Input.GetButton ("Fire1")){
         InvokeRepeating("Shoot", 0/*start firing imediately (no delay)*/, ROF); //start calling the Shoot function
     }else{
         CancelInvoke("Shoot");
     }
 }
 
 function Shoot() {
     Instantiate(Bullet, GunObj.position, GunObj.rotation); //creates a new 'bullet' every time this function is called
 }

Scribe

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 timo0060 · May 16, 2012 at 06:24 PM 0
Share

This doesn't do anything that my code was supposed to do. $$anonymous$$y code was to move my bullets, your's are shoot my bullet. But your code helps a lot because it'll allow me to shoot easier. Thank you very much for this lovely piece of code which is going to replace my current script. Cheers $$anonymous$$ate

avatar image timo0060 · May 16, 2012 at 07:11 PM 0
Share

There's just one problem with your code. When I fire, it's a continuous stream ins$$anonymous$$d of the 1 every 0.5 seconds that the rate of fire is set up to. Any idea how to fix that?

avatar image Scribe · May 29, 2012 at 01:19 PM 0
Share

sorry for the late reply, It does that because I was being a bit stupid, the if statement in the update should probably be...


if(Input.GetButtonDown("Fire1")){

   InvokeRepeating("Shoot", 0, ROF);

}

if(Input.GetButtonUp("Fire1")){

  CancelInvoke("Shoot");

}

Hopefully that works and glad it helps despite being unrelated to your question! sorry bout that

avatar image timo0060 · May 29, 2012 at 01:51 PM 0
Share

Thank you. It's better late then never, and I did end up getting it working. Thank you for all the help you gave.

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

8 People are following this question.

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

Related Questions

Enemies Shoot Towards Face, Would Like to have them Shoot to Chest 1 Answer

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

Bullets follow mouse after shooting in Sidescroller 1 Answer

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

Clones of enemies will not shoot 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