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
2
Question by Ilkzz · Mar 11, 2014 at 10:41 PM · c#2dshootingdirectiontransform.

2D Shooting right only. Doesn't flip direction with character

Hi,

My situation is that I have a 2D platformer. The shooting works, but only towards the right direction. I have attached the script to a empty game object to position the shooting position exactly to where I want. I have tried several ways; trying to add another direction and trying to flip the child, all which I couldn't do.

I have 3 different scripts that I use to do this...

My First:

 public class InfitityMovement : MonoBehaviour {
 
     public Vector2 speed = new Vector2(10, 10);
 
     public Vector2 direction = new Vector2(-1, 0);
     private Vector2 movement;
 
     
     // Update is called once per frame
     void Update () {
         movement = new Vector2(
             speed.x * direction.x,
             speed.y * direction.y);
     
     }
 
     void FixedUpdate()
     {
         // Apply movement to the rigidbody
         rigidbody2D.velocity = movement;
     }
 }

The Second:

 public class PlayerShooting : MonoBehaviour {
     
     public Transform bullet;
 
     public float shootingRate = 0.25f;
 
     private float shootCooldown;
     
     void Start()
     {
         shootCooldown = 0f;
     }
     
     void Update()
     {
         if (shootCooldown > 0)
         {
             shootCooldown -= Time.deltaTime;
         }
     }
 
     public void Attack(bool isEnemy)
     {
         if (CanAttack)
         {
             shootCooldown = shootingRate;
             
             // Create a new shot
             var shotTransform = Instantiate(bullet) as Transform;
             
             // Assign position
             shotTransform.position = transform.position;
             
             // The is enemy property
             ShootingControl shot = shotTransform.gameObject.GetComponent<ShootingControl>();
             if (shot != null)
             {
                 shot.isEnemyShot = isEnemy;
             }
             
             // Make the weapon shot always towards it
             InfitityMovement move = shotTransform.gameObject.GetComponent<InfitityMovement>();
             if (move != null)
             {
                 move.direction = this.transform.right; // towards in 2D space is the right of the sprite
             }
         }
     }
 
     public bool CanAttack
     {
         get
         {
             return shootCooldown <= 0f;
         }
     }
 }

Lastly...

 public class ShootingControl : MonoBehaviour {
 
     public int damage = 1;
 
     public bool isEnemyShot = false;
 
     // Use this for initialization
     void Start () {
         Destroy (gameObject, 10);
     
     }
 }

I have tried playing around with the transform.right section, didn't work. I tried changing the Vector2 direction variable to different numbers, didn't work. Ive just been clueless all day!

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 Ilkzz · Mar 11, 2014 at 10:45 PM 0
Share

Side note, my character flips to direction using localScale

avatar image robertbu · Mar 11, 2014 at 10:50 PM 0
Share

What I don't have to answer your question is how you want to aim. Do you want to aim at a target, or is there some controls that should control the direction of aim, or....

This line if code is what is causing your shooting to be to the right:

  move.direction = this.transform.right; 

Actually it is using the right of the object this script is attached to (empty game object), not the world right. I'll give you a quick thing to play with so that you can see that you can shoot in other direction, but I'm not sure of the right fix:

  move.direction = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;

Hard code or set 'angle' to some value to set the direction. A value of 0, will shoot right.

avatar image robertbu · Mar 11, 2014 at 10:53 PM 0
Share

Take #2: I read your title and your comment that came through while I was commenting. I$$anonymous$$HO, the right fix is to make sure the empty game object is a child of the sprite, then change your rotation code to rotate 180 on the 'Y' rather than to use localScale. You could use the the AngleAxis() solution with your current code, but you'd also have to move your hit point when you flipped your character.

avatar image Ilkzz · Mar 11, 2014 at 11:27 PM 0
Share

Hi, thanks for the quick reply. I have just tried to implement the changes. As you said, the Quaternion angle does change its direction back and forward, but I realised it doesn't flip the whole sprite around (a backwards rocket just doesn't work)

Also, the AngleAxis works, but also flips my camera around. Is there any way of stopping the camera from moving because it is a child of the main character so it follows it around the map. The projectile again moves to the original side

avatar image dqflynn · Mar 14, 2014 at 01:11 AM 0
Share

stopping the camera from rotating with your character, go to Roll a ball tutorial. It will explain that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dqflynn · Mar 11, 2014 at 11:37 PM

if you attached it to an empty GameObject, then I think I see your problem. When you tell things to go to the right or left of an object, they go to the right or left of the object. I think your problem is that you are shooting from a different point. The wrong point. I have 3 ideas for you: 1. make the E.G.O. (empty gameobject) a child of your player. 2. put the script in the character, not the E.G.O. 3. make the script flip whenever the player flips, by making the left arrow key not just move, but rotate the shooter as well. Hope this works!

Comment
Add comment · Show 3 · 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 Ilkzz · Mar 12, 2014 at 12:18 AM 1
Share

$$anonymous$$y current set up is your first idea, the empty game object is the child of the character.

The second way, does exactly the same as my original problem. The shooting still only goes to the right. I have been attempting your 3rd idea for a while, but to failure

avatar image dqflynn · Mar 12, 2014 at 12:40 AM 0
Share

I'm sorry that it's not working out. I'm not an experienced programmer by a long shot, I just have ideas.

avatar image dqflynn · Mar 12, 2014 at 12:42 AM 0
Share

(BTW, if anyone knows how to do my 3rd idea, if you put the script here it would help lots!)

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

RayCast2D direction is always showing origin point(0,0,0) on game and ignores direction Vector2D.(Unity 2017.4.7.f1) 0 Answers

Cinemachine camera rotating with player 2D 1 Answer

Fire bullet into the direction my player is facing 2D 2 Answers

2d fire from player towards mouse 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