- Home /
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!!
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 :)
#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
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.
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
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)
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?
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;
}
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
Your answer
Follow this Question
Related Questions
Setting parent of instantiated sprite 2 Answers
How to instantiate a new GameObject (sprite) properly? 1 Answer
Instantiate - Prefab as Child 1 Answer
Changing the sprite of an instantiated object through a collision box from another gameobject. 1 Answer
[2D] - Moving the child moves the parent <-- Unwanted behavior 1 Answer