- Home /
How do i shoot in the direction im facing?
Hi guys I've tried everything that I could think of but sadly im a huge noob. Can anyone help figure out why I cant shoot to the left. This is my shoot script attached to the shoot point.
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody2D;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(20, 0, speed));
projectile.velocity = new Vector2(velX, velY);
}
}
}
Here is my Controller script.
{
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
void Update()
{
Vector2 move = Vector2.zero;
float horizontal = Input.GetAxis("Horizontal"); // a or left = -1 d or right = 1
rb.velocity = new Vector2(horizontal * Speed, rb.velocity.y);
animator.SetFloat("Speed", Mathf.Abs(horizontal));
if (Input.GetButtonDown("Jump") && grounded)
{
velocity.y = jumpTakeOffSpeed;
}
else if (Input.GetButtonUp("Jump"))
{
if (velocity.y > 0)
{
velocity.y = velocity.y * 0.5f;
}
}
animator.SetBool("grounded", grounded);
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
targetVelocity = move * maxSpeed;
}
}
Answer by Vega4Life · Jul 31, 2019 at 05:28 PM
So when you do a flip on a spriteRenderer, it doesn't change the objects orientation. Thus when yo do something like, (transform.TransformDirection(new Vector3(20, 0, speed)); ) this will still shoot towards the right.
You could manually keep track of that flip in an int flipState, where 1 is to the right, -1 is to the left. Change the int when you flip the renderer. Then just multiply this int value to the transform.TransformDirection(new Vector3(20, 0, speed)) * flipState; Where a positive transform is right, negative is left.
Hopefully that makes sense.
This all your doing really. You declare an int, then just change the value of it when you flip the renderer:
// This is declared at the top of your script
int flipState = 1; // We start looking to the right (by default)
// Then change it if you flip your renderer
mySpriteRenderer.flipX = false;
flipstate = 1;
mySpriteRenderer.flipX = true;
flipstate = -1
// Then later, multiply the flipstate to your transform.transformdirection
Answer by ReubenClare123 · Jul 31, 2019 at 05:16 PM
This is my flip script as well:
// variable to hold a reference to our SpriteRenderer component private SpriteRenderer mySpriteRenderer;
// This function is called just one time by Unity the moment the component loads
private void Awake()
{
// get a reference to the SpriteRenderer component on this gameObject
mySpriteRenderer = GetComponent<SpriteRenderer>();
}
// This function is called by Unity every frame the component is enabled
private void Update()
{
if (Input.GetKeyDown(KeyCode.D))
{
// flip the sprite
mySpriteRenderer.flipX = false;
}
// if the A key was pressed this frame
if (Input.GetKeyDown(KeyCode.A))
{
// if the variable isn't empty (we have a reference to our SpriteRenderer
if (mySpriteRenderer != null)
{
// flip the sprite
mySpriteRenderer.flipX = true;
}
}
}
}
Answer by MounirDev · Jul 31, 2019 at 07:51 PM
@ReubenClare123 Quaternion.identity :) if you want to shoot a bullet in front of you character or gun you do simply : Instantiate(bulletPrefab, transform.position, Quaternion.identity); this will set the bullet rotation like you character's. then just give your bullet game Object a scrip to move forward.