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 rOBY GAMES · May 09, 2015 at 08:13 AM · 2dprefabgamefire

Change script from 3d to 2d Game.

Hello everyone.

Currently I have a script Which fires projectiles That works well in 3D.

I need help to change it in 2d shot. I added Rigidbody2D. By testing the script works, but the shot does not travel on the axis x or -x. the shot explodes in front of the character in 2D without traveling on the axis X. How can you change this script from 3d to 2d shot?

 #pragma strict
  
 var Prefab : Transform;
 var PrefabSpeed : float = 6000;
 
 function Update () 
 {
  if(Input.GetButtonDown("Fire1")) 
 {
    if(Collisions.GRENADE_AMMO > 0)
 
 
 if(!Prefab || !PrefabSpeed) 
    {
 
      Debug.Log("Il prefab o la velocità non esiste");
 }
   else
    {
 var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
   PrefabCreate.rigidbody2D.AddForce(transform.forward * PrefabSpeed);
      
      Collisions.GRENADE_AMMO --;
      GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_AMMO;
      print("YOU NOW HAVE " + Collisions.GRENADE_AMMO + " GRENADES");
      }
    }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fappp · May 09, 2015 at 09:02 AM

Use this:

PrefabCreate.rigidbody2D.AddForce(transform.forward * PrefabSpeed, ForceMode2D.Impulse);

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 rOBY GAMES · May 09, 2015 at 09:23 AM 0
Share

Thanks for the reply.

I tried this,PrefabCreate.rigidbody2D.AddForce (transform.forward * PrefabSpeed, Force$$anonymous$$ode2D.Impulse ); yet but the shot remains stationary.

avatar image Fappp · May 09, 2015 at 09:50 AM 0
Share

Is the rigidbody set to kinematic?

avatar image rOBY GAMES · May 09, 2015 at 10:02 AM 0
Share

Yes. the rigidbody is kinematic. Now when the character jumps on the normal shot. But when idle, the shot explodes. the problem should be in the Projectile Spawn, vove part by the blow.

avatar image rOBY GAMES · May 09, 2015 at 10:14 AM 0
Share

I moved the Projectile Spawn, at the top is now working. But itself I move too far down the shot explodes. I do not understand the problem. :-(

avatar image
0

Answer by Davrinsky-sky · Jul 06, 2017 at 12:40 AM

I'm wondering if you are shooting your projectile in the axis that is parallel to your main camera when you need perpendicular if the projectile is behind your 2D character. Try shooting out on the axis that is perpendicular to your main camera view. I don't know if you want to rearrange your whole scene on a different axis or have your script change the projectiles to go along a different axis when in 3D.

just an offhand thought.

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 Jwizard93 · Jul 05, 2017 at 10:01 PM

Are you sure that transform.forward is the x-Axis? It should probably be transform.right. Also I suggest you clean this up. If it were any more complex than it is I wouldn't have read it for its messiness.

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

22 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

Related Questions

Shooting 2D - how to fix 1 Answer

Changing prefabs to 2D images 0 Answers

throw / fling object or prefab 1 Answer

Is is possible to replace a prefab with a variant in script? 1 Answer

Changing SpriteRenderer order in script 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