- Home /
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
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
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.
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.
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.
"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 ?
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?
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");
}
}
}
}
This should be edited into your question, not posted as an answer.
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