- Home /
Difficulty with my tank game
I have a game (top down 2d) and i have several problems The whole project can be downloaded here https://www.mediafire.com/?f0n0grifi7m5ccg I have 5 scripts and the problems are the following: Script #1 Move Problem:The tank never rotates to the right when i press "D"
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
public float speed;
public float rotspeed;
void Update () {
if (Input.GetKey (KeyCode.W))
transform.Translate (new Vector2 (0, speed * Time.deltaTime));
if (Input.GetKey (KeyCode.S))
transform.Translate (new Vector2 (0, -speed * Time.deltaTime));
if (Input.GetKey (KeyCode.A))
transform.Rotate (new Vector3 (0,0, rotspeed * Time.deltaTime));
if (Input.GetKey (KeyCode.D))
transform.Translate (new Vector3 (0,0, -rotspeed * Time.deltaTime));
}
}
Script #2 Shell Problem:The tanks bullets clone Shell(clone) should ignore collision with the tank(because the bullet has the script and it gets cloned)
using UnityEngine;
using System.Collections;
public class Shell : MonoBehaviour {
public Transform Hull;
void Start() {
Physics.IgnoreCollision(Hull.collider, collider);
}
}
Script #3 Health Problem:**The tank has a HP system(very basic),the tank should take damage from the Shell2(Clone)<- The enemys bullets clone(Not tested yet-no enemy yet-)but
the tank disappears after entering playmode after 2-3seconds or not and it gets a -12 z transform(A 2D GAME) here is the script
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float health;
public float dam;
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.name == "Shell2(Clone)")
{
health = health-dam;
}
if (health <= 0)
Destroy (this.gameObject);
}
}
The other scripts and the whole project can be downloaded here https://www.mediafire.com/?f0n0grifi7m5ccg
Thanks if you can help me Oh and what do you think of the game thats my first one? The other scripts and the whole project can be downloaded here https://www.mediafire.com/?f0n0grifi7m5ccg
Im not sure why your tank isnt rotating when you press d but in Unity its much better to use Input.GetAxis ins$$anonymous$$d of Get$$anonymous$$ey. This gives better support for multiple devices and gamepads and also gives the player the choice to customize their buttons. By default the vertical and horizontal axis are set to WASD and the arrow keys. You could say
transform.Translate(new Vector3(Input.GetAxis("Vertical") * time.deltaTime,0,0)
and that one line of code would take care of both the W and D keys
With Question 2 I would try looking into layer based collision detection which is easy to set up http://docs.unity3d.com/$$anonymous$$anual/LayerBasedCollision.html
Comment #1 I am using vector3 ins$$anonymous$$d of vector2 cuz it's a top down game and the tank is rotating along the Z axis.
Comment #2 Thanks i'll take a look at it.
Don't delete questions that are solved, please leave posted so others can learn from the answer. It's the least you can do in return for the free support.
@Bored$$anonymous$$ormon - I tried to do that rollback to the question earlier but couldn't - how did you do it?!
Answer by Pyrian · Aug 07, 2014 at 11:10 PM
Your code for "D" should probably use rotate like it does for "A" instead of Translate like it's using in your code snippet.
I don't know, but are you absolutely sure that Hull is set correctly in these clones?
I can't tell from this what's killing your tank, but I'm pretty sure your negative Z-value is coming from the Translate in issue #1...
Answer by Foose · Aug 10, 2014 at 08:13 PM
I just googled "unity Tank 2d movement script" and google gave me this tutorial. I guess thats pretty much what you are looking for ;) http://wiki.unity3d.com/index.php/SimpleTankController
Your answer
Follow this Question
Related Questions
Enemy Knockback when hit player 2D Topdown 1 Answer
2D camera zoom smoothing and limitations? 1 Answer
Echolocation in 2D top down game 0 Answers
Multiple Cars not working 1 Answer