• 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 Madswint · Aug 12, 2014 at 12:54 PM · 2dinstantiatetransformsprite

How can I make a 2d sprite shoot the direction player is facing?

I have a variable that checks if it is looking right or left (true = right, left = false), and I've tried the for hours to get it to "fire" the sprite ball in that direction after instantiating it, but it just doesn't work. I'm at the point where I just dont know what to do anymore. Have anyone had this problem with 2d shooting? If so I would appreciate it if you could share what you did.

TL;DR, I'm trying to make a ball fire the correct Z direction. Thank you!!

Comment
Add comment · Show 5
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 DajBuzi · Aug 12, 2014 at 01:01 PM 0
Share

If your projectlie has rigidbody component then you can just say:

 Rigidbody2D projectile = (Instantiate(obj, pos, rot) as GameObject).GetComponent<Rigidbody2D>();
 projectile.velocity = (right ? Vector2.right : -Vector2.right);

I think this should work :)

avatar image Madswint · Aug 12, 2014 at 01:34 PM 0
Share
 #pragma strict
 
 var BubblePrefab : Transform;
 var BubbleSpawn : Transform;
 
 var FireSound : AudioClip;
 var spawnDistance : float = 10.0f;
 function Start () {
 
 }
 
 function Update () {
 
 
 
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
 
     
  
    
 
   Rigidbody2D projectile = (Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation) as GameObject).GetComponent<Rigidbody2D>();
 projectile.velocity = (right ? Vector2.right : -Vector2.right);
 
      
      
 }
 }

Here is my script, can you in any way help me further? I dont know what to put in some of the spots

avatar image bob1234 · Aug 12, 2014 at 01:53 PM 0
Share

I just posted an answer, but it's not showing?

avatar image Madswint · Aug 12, 2014 at 01:59 PM 0
Share

It's not showing

avatar image bob1234 · Aug 12, 2014 at 02:05 PM 0
Share

should show now :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by misticone00 · Aug 12, 2014 at 02:12 PM

Hi,

Why don't you just do:

 public Sprite FireSprite;
     void Update () {
         if (Input.GetKeyDown ("a")) {
             Instantiate(FireSprite,transform.position,transform.rotation);}
     }

Attatch that script to the player (shooter)

Create another script and attatch it to the FireSprite :

 public int direction;
 
 void Start () {
             if (Player.DirectionFacing == "Right") {
                     direction = 1;    
     
             } else if (Player.DirectionFacing == "Left") {
                     direction = 2;

             }

     }

 void Update () {
     if (direction == 1) {
     
                     transform.Translate (0, 0, Time.deltaTime);
     
             } else if (direction == 2) {
             
         transform.Translate (0,0,-Time.deltaTime);
     }

 }



I didn't test the code so i don't know if it will work, let me know if it has any problem. I hope i could help.

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 bob1234 · Aug 12, 2014 at 02:00 PM

I have done like this: (this is in c#)

     public  Vector2 moving = new Vector2();
     bool lookRight;
 void Update()
 {
                 if(Input.GetKey("d"))
                 {
              moving.x = 1;
                 }
             if(Input.GetKey("a"))
                 { moving.x = -1;
                 }
             if(moving.x > 0)
             {
             lookRight = true;
             }else if(moving.x < 0)
             lookRight = false;
 }
             void Fireball()
             {
             if(lookRight)
             Rigidbody2D projectile = Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation) as Rigidbody2D;
             projectile.AddForce(vector3.right * 500)
             }else{
             Rigidbody2D projectile = Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation) as Rigidbody2D;
             projectile.AddForce(vector3.right * 500)
             }
     }

and then just call Fireball() where you have your keypress

Comment
Add comment · Show 8 · 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 Madswint · Aug 12, 2014 at 02:06 PM 0
Share

It gives me

InvalidCastException: Cannot cast from source type to destination type. BasicAttack.Fireball () (at Assets/BasicAttack.js:23) BasicAttack.Update () (at Assets/BasicAttack.js:17)

avatar image bob1234 · Aug 12, 2014 at 02:13 PM 0
Share

Hmm, i'm not sure what it is, but what i wrote was in c# and it looks like ur using javascript, did u convert it or just copy paste?

avatar image Madswint · Aug 12, 2014 at 02:18 PM 0
Share

I converted it all and it's working now. Thank you. Do you have any idea on jumping as 2d? Here's my controls incase you can help

(move left and right, I need jump and perhaps doublejump)

 var maxSpeed = 5;
 static var FacingRight = true;
 private var myRigidbody : Rigidbody;
 
 function FixedUpdate () {
 var move = Input.GetAxis("Horizontal");
 
 rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
 
 if (move > 0 && !FacingRight) 
 Flip();
 else if(move < 0 && FacingRight)
 Flip();
 }
 
 function Flip () {
 
 FacingRight = !FacingRight;
 
 var theScale : Vector3 = transform.localScale;
 theScale.x *= -1;
 transform.localScale = theScale;
 }
 

avatar image bob1234 · Aug 12, 2014 at 02:26 PM 0
Share

Yeah sure man, do

 int maxJump = 2;
 int jumpCount = 0;
 bool grounded;
 public float jumpForce = 325f;
 
 void FixedUpdate()
 {
 var absVelY = $$anonymous$$athf.Abs(rigidbody2D.velocity.y); // this takes the absolute value of the velocity's y value and returns a positive value, whatever its positive or negative
 
 if(absVelY < 0.02f)
 {
 grounded = true;
 }else
 {
 grounded = false;
 }
 if(grounded)
 {
 jumpCount = 0;
 }
 
 if(jumpCount < maxJump)
 {
 if(Input.Get$$anonymous$$eyDown("space"))
 {
 rigidbody2D.AddForce(new vector2 (0, jumpForce));
 jumpCount++;
 }
 }
 }

thats my jumping code i use

avatar image Madswint · Aug 12, 2014 at 03:36 PM 0
Share

It resets in air, do you have this problem too?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 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

Related Questions

Why does transform.Rotate not move the sprite and BoxCollider2D together? 1 Answer

transform.position not doing anything 1 Answer

Sprite is not shown moving 1 Answer

Achieving something like LookAt but with the X axis. 3 Answers

Unity 5.1.1 Transform Lag/Bug 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