- Home /
Game Object is moving towards the left when fire button is pressed.
I'm currently making a Space Invader clone as a practice I used Rigidbody on the ship. The problem that I'm getting is whenever I pressed the fire button the Ship moves towards the left every time it shoots. Choosing Kinematic fixes this, however this ignores the box collider that I placed on to act as a border for the ship.
I know that there are better ways to execute this properly but I'm currently experimenting if there is a way to remove the force.
My question is how can I remove the force being added to the ship?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class ship_control : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D ship;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
// Start is called before the first frame update
void Start()
{
ship = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal")>0.5f||Input.GetAxisRaw("Horizontal")<-0.5)
{
ship.velocity = new Vector2 (Input.GetAxisRaw("Horizontal") * moveSpeed, ship.velocity.y);
}
if (Input.GetAxisRaw("Horizontal")<0.5f&&Input.GetAxisRaw("Horizontal")>-0.5)
{
ship.velocity = new Vector2(0f, ship.velocity.y);
}
if (Input.GetButton("Fire1")&&Time.time>nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot,shotSpawn.position,shotSpawn.rotation);
}
}
}
I would advise you to take a look into the 2D shooter tutorial series. It explains exactly how to move, shoot and set boundaries in a proper way. It helped me a lot at the beginning. https://unity3d.com/de/learn/tutorials/projects/space-shooter-tutorial/moving-player?playlist=17147
Answer by highpockets · Apr 09, 2019 at 08:12 AM
Does your projectile spawn inside the collider of the ship?? Try offsetting the spawn position a bit or scaling down the ship’s collider
Yes I tried playing with the collider and it does affect the movement, thank you for that hint. How do I change the spawn position of the bullet lets say above the ship? Currently the bullet is spawning inside the ship or in the same layer the ship it's on.
I’m assu$$anonymous$$g that you are shooting in the forward direction of the ship (the local z/blue axis), depending how far away you want it to spawn. You use the transform.forward which is a normalized (1 unit in length) vector and multiply it by the distance away that you want it to spawn and add that to the transform.position.
Or you can place an empty game object at the exact spot that you want to spawn the bullet and make that a child of your ship, then just spawn at the empty game object’s location. I changed my comment to an answer as it appears that was the issue
Answer by zakkaiokenx10 · Apr 09, 2019 at 09:10 AM
Make sure your not spawning the bullet inside the ship's collider. saying that youre spawning at a spawnshot gameobject's location, move spawnshot out of the collider of the ship