I have a 2D Sidescroller and i need to instantiate a bullet and the bullet will move to what ever direction the player is facing.
The problem is lets say the player shoots and the bullet goes left and once the player moves RIGHT the bullet will also move RIGHT. This is probably due to Update(), but im not sure what else to use.
In Bullet Script;
void Start () {
player = GameObject.FindGameObjectWithTag(“player1”);
}
// Update is called once per frame
void Update () {
CharMovement movementscript = player.GetComponent<CharMovement>();
if(movementscript.isRight){
transform.Translate(Vector3.right * bulletSpeed*Time.deltaTime);
}
if(!movementscript.isRight){
transform.Translate(Vector3.left* bulletSpeed*Time.deltaTime);
}
}
In Player Script
if (Input.GetKey(KeyCode.A)) {
state = WalkingState.WalkLeft;
}
else if (Input.GetKey(KeyCode.D)) {
state = WalkingState.WalkRight;
}
else if (Input.GetKeyDown(KeyCode.S)){
if(isRight){
Instantiate(bullet,transform.position,transform.rotation);
}
else if(!isRight){
Instantiate(bullet,transform.position,transform.rotation);
}