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 Aladine · Aug 11, 2013 at 04:52 PM · transformpositionz axiszero

Keep position.z always at 0

Hello,

am still working on the 2D shooter game, now am in the part where i should check for collisions between enemies,player and bullets, the thing is that the collision works but not for the bullets, somehow they change their position at the Z axis so they will not collide with the the enemies, am sure that the mistake is in one of my transform codes so am going to past it here (tell me if you need the full class, i don't want to make the post look full for nothing) : i thought you may need to see how the player move (maybe it helps)

   //this is how i move the ship 
             if (move) {
                 if (Input.GetButton ("Fire1")) {
                     float distance = myTransform.position.z - cam.transform.position.z;
                     targetPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
                     targetPosition = cam.ScreenToWorldPoint (targetPosition);
                     // Rotation code;
                     Vector3 look = targetPosition - transform.position;
                     if (look != Vector3.zero) {
                         myTransform.rotation = Quaternion.LookRotation (look, Vector3.back);
                         
                     }
     }
     myTransform.position = Vector3.MoveTowards (myTransform.position, targetPosition, speed * acceleration * Time.deltaTime);
 

and this is the shooting method, as you see i tried to set pos.Z always to zero, but it's not working :

 //this is the shooting code
         void shoot (float reload, int num, float distance)
         {
             tmpReload += 0.1f;
             if (tmpReload >= reload) {
                 for (int i =0; i<num; i++) {
                     Quaternion target = Quaternion.AngleAxis ((distance * (i - (num / 2))), transform.up);
                     Vector3 pos;
                     pos.x = myTransform.position.x;
                     pos.y = myTransform.position.y;
                     pos.z = 0;
                     Instantiate (BulletFab, pos, target * myTransform.rotation);
                 }
                 tmpReload = 0;
             }
         }

and finally here is the how i move the bullets (attached to the prefab) :

   myTransform.position.Set (myTransform.position.x, myTransform.position.y, 0);
             myTransform.Translate (Vector3.forward * speed * Time.deltaTime);

i believe it's because of "Vector3.forward" isn't ? but i can't find a way to replace it thank you very much

Comment
Add comment · Show 1
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 Aladine · Aug 11, 2013 at 04:55 PM 0
Share

i have noticed that when shooting only one bullet, position.z is always at 0, but as long as i start shooting more than one bullet at once, it start changing

2 Replies

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

Answer by Slobdell · Aug 11, 2013 at 05:22 PM

Edit: changing answer to reflect real answer.

The problem was in 2D space the bullet was sneaking by the collider because the collider was too thin. Enlarged collider to prevent bullet from missing the collider even though they were technically supposed to be colliding.

Comment
Add comment · Show 10 · 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 Aladine · Aug 11, 2013 at 06:02 PM 0
Share

thank you for replying, but no it still the same thing

avatar image Slobdell · Aug 11, 2013 at 06:13 PM 0
Share

Can you post all the code? It's hard to figure stuff like this out if you don't know what order everything is happening in.

avatar image Slobdell · Aug 11, 2013 at 07:07 PM 0
Share

I just reread your original post and noticed that this is a 2D shooter game. I'm not sure what world axis you are moving on from left to right but if this is 2D then you will want your bullet to only move on one access, correct? It should be going left to right, or right to left. Not up or down, and not closer or further from the camera.

So I'm thinking that you think it's the Z axis that is changing, when in reality it is the x or y depending on the orientation of the game. I think you need to figure out exactly which ONE axis you want the bullet to move on, and BOTH the other two should be set to a constant(either 0 or maybe like 5 since you don't want it to be on the ground.

Does that make sense, if I understand what you're doing correctly.

avatar image Aladine · Aug 11, 2013 at 07:16 PM 0
Share

"It should be going left to right, or right to left. Not up or down, and not closer or further from the camera." excuse me but am not following you here, yes it shouldn't get closer or further, but why only Left/right or up/down ? am still new to unity but i made some flash games before, and to make an object move "forward" (depending on its rotation) i do change both of the x and y position, so can you please explain more ? PS : does it help if i upload the project for you ?

avatar image Slobdell · Aug 11, 2013 at 08:01 PM 0
Share

So if your game is 2D, then there should be no Z. So you're setting Z to 0 which is good so it will always be the same distance from the camera. So if the Z axis were getting screwed up the bullet would look like it was disappearing off into the distance straight ahead of you, or hitting the camera. So that leaves us with only the X and Y. X will be your bullet moving left to right, and Y will be your bullet moving up/down. So it sounds to me like if your bullet is going off the line you thought, i.e., moving up or down and not hitting your target, it's because the Y axis is getting changed, not the Z axis. I don't know, can you tell me what you're expecting the bullet to do and what it actually is doing? Like, you're shooting straight right and it goes on an angle up over the enemy, or it hits the ground, or it disappears?

Show more comments
avatar image
-1

Answer by Aladine · Aug 11, 2013 at 06:47 PM

Full codes :

Bullet.cs (attached to the bullet prefab) : using UnityEngine; using System.Collections;

 public class Bullet : MonoBehaviour
 {
     
     
     private Transform myTransform;
     private float speed;
     
     // Use this for initialization
     void Start ()
     {
     
         speed = 12;
         myTransform = transform;
         myTransform.position.Set (myTransform.position.x, myTransform.position.y, 0);
     
         
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         
         myTransform.Translate (Vector3.forward * speed * Time.deltaTime);
         myTransform.position.Set (myTransform.position.x, myTransform.position.y, 0);
     
         if (!renderer.isVisible) {
             Destroy (gameObject);
         }
     }
     
     void OnTriggerEnter (Collider collider)
     {
         
         if (collider.gameObject.CompareTag ("stand_enemy")) {
             print ("kill");
         }
     }
 }


ShipControl.cs (attached to the player object) :

         private Transform     myTransform;            //transform cache    
         public GameObject     BulletFab;                 // bullet prefab
         
         Vector3             targetPosition;         //place to go 
          
         public     float         speed;                    //general speed   
         public     float         acceleration;            //acceleration to add 
         public     float        accelerationLimits;        //max acceleration 
         
         public     float         reload ;                //timer to reload bullet
         private float         tmpReload ;
         public     int            numOfBullets;            //number of bullets in one shot
         public     float         distanceBetween;        //distance between bullets
         
         public    bool         move ;                     //can move 
         public     bool         changeMove;                //GUI settings
     
         
         void Start ()
         {
             myTransform = transform;
             //Moving settings
             speed = 3;
             accelerationLimits = 3;
             acceleration = 1;
             move = true;
             changeMove = true;
             bulletSpeed = 12;
             
             //positon settings
             targetPosition = myTransform.position;
             myTransform.rotation = myTransform.rotation;
             
             //shooting settings  
             reload = 1;
             numOfBullets = 5;
             tmpReload = 0;
             distanceBetween = 10;
             
             //initiating settings
         
             if (cam == null) {
                 cam = Camera.main;
             
             }
         }
      
         void Update ()
         {
             //this is how i move the ship 
             if (move) {
                 if (Input.GetButton ("Fire1")) {
                     float distance = myTransform.position.z - cam.transform.position.z;
                     targetPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance);
                     targetPosition = cam.ScreenToWorldPoint (targetPosition);
                     // Rotation code;
                     Vector3 look = targetPosition - transform.position;
                     if (look != Vector3.zero) {
                         myTransform.rotation = Quaternion.LookRotation (look, Vector3.back);
                         
                     }
                 
                     if (acceleration < accelerationLimits) {
                         acceleration += 0.2f;
                     } else if (acceleration >= accelerationLimits) {
                         acceleration = accelerationLimits;
                     }
                 
                 
                     if (myTransform.position == targetPosition) {
                         acceleration -= 0.1f;
                     }
                 
                     
                 
                 } else {
                     if (acceleration > 1) {
                         acceleration -= 0.1f;
                     }
                 }
             
             }
             myTransform.position = Vector3.MoveTowards (myTransform.position, targetPosition, speed * acceleration * Time.deltaTime);
             shoot (reload, numOfBullets, distanceBetween);
             changeSettings ();
         }
         //this is the shooting code
         void shoot (float reload, int num, float distance)
         {
             tmpReload += 0.1f;
             if (tmpReload >= reload) {
                 for (int i =0; i<num; i++) {
                     Quaternion target = Quaternion.AngleAxis ((distance * (i - (num / 2))), myTransform.up);
                     Vector3 pos;
                     pos.x = myTransform.position.x;
                     pos.y = myTransform.position.y;
                     pos.z = 0;
                     Instantiate (BulletFab, pos, target * myTransform.rotation);
                 }
                 tmpReload = 0;
             }
         }
         
         void OnMouseOver ()
         {
             move = false;
         }
         
         void OnMouseExit ()
         {
             move = true;
         }
         
         
         void OnTriggerEnter (Collider collider)
         {
             
             if (collider.gameObject.CompareTag ("stand_enemy")) {
                 print ("daed");
             }
         }
     }
 }

Comment
Add comment · Show 2 · 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 cmpgk1024 · Aug 11, 2013 at 08:12 PM 0
Share

This should be edited into your question, not posted as an answer.

avatar image Aladine · Aug 11, 2013 at 08:58 PM 0
Share

sorry i shouldn't do that, (am used to Forum discussion, actually it's the first time i use an "answer" website)it will not happens again

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

17 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

Related Questions

Using the Rule Set function from the PiXYZ Plugin to try to reset Position or Translation with the 'Set > Transform' node without resetting the Rotation field either 0 Answers

make player move in direction it's facing 2 Answers

What is the reasoning of multiplying by Time.deltaTime 1 Answer

does not work check if dead 1 Answer

Help around an approach to a "Virtual Motion Table" in Unity 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