how to shoot on different direction while moving away
so i'm making a third person shooter on android, where left analog will move and rotate the character as i move it, and right analog to shoot with direction.
while standing still,the right analog does the job of shooting with the right direction, but while moving, the rotation created by the left analog can't be overwrite by the right one.
can somebody help me with this ?
i did try using public static bool on_shoot, but when Debug it, it always false on the player script. while it's okay on the bullet_spawner
you can see Supercell's brawlstar for their analog and shooting movement for reference.
// this is player script
void Update()
Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
if (moveVector != Vector3.zero)
{
Debug.Log(sc_Bullet_Mouth.on_shoot);
transform.rotation = Quaternion.LookRotation(moveVector);
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
}
}
//this is bulletSpawner script
Public class sc_Bullet_Mouth : MonoBehaviour
{
public Joystick right_joystick;
public GameObject Bullet;
public GameObject player;
public float fireRate;
public float nextFire;
public bool canshoot;
public static bool on_shoot;
// Start is called before the first frame update
void Start()
{
on_shoot = false;
fireRate = 0.75f;
nextFire = 0.0f;
canshoot = true;
}
// Update is called once per frame
void Update()
{
Vector3 rightMoveVector = (Vector3.right * right_joystick.Horizontal + Vector3.forward * right_joystick.Vertical);
if (rightMoveVector != Vector3.zero)
{
on_shoot = true;
Debug.Log("masuk" + on_shoot);
player.transform.rotation = Quaternion.LookRotation(rightMoveVector);
if( Time.time > nextFire)
{
StartCoroutine(fire());
nextFire = Time.time + fireRate;
}
on_shoot = false;
}
}
public IEnumerator fire()
{
Instantiate(Bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.09f);
Instantiate(Bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.09f);
Instantiate(Bullet, transform.position, transform.rotation);
}
}
Answer by Skaster87 · Feb 28, 2019 at 05:32 PM
I'm guessing your immediately overwriting any changes in your update loops, and this is why I'm not a fan of changing the player rotation from the SC_Bullet_Mouth script, but assuming sc_Bullet_Mouth works how I think, you are correct in saying you need a bool. Something like this should work.
public class PlayerScript: MonoBehaviour {
sc_Bullet_Mouth bullet_Mouth; // Create reference to bullet_mouth component
void Start() {
bullet_Mouth = GetComponent<sc_Bullet_Mouth>();
}
void Update() {
Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
if (moveVector != Vector3.zero)
{
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
if (!bullet_Mouth.isShooting)// if not shooting
{
transform.rotation = Quaternion.LookRotation(moveVector);
}
}
}
}
//this is bulletSpawner script
public class sc_Bullet_Mouth : MonoBehaviour
{
public Joystick right_joystick;
public GameObject Bullet;
public GameObject player;
public float fireRate;
public float nextFire;
public bool canshoot;
public bool isShooting;
// Start is called before the first frame update
void Start()
{
isShooting = false;
fireRate = 0.75f;
nextFire = 0.0f;
canshoot = true;
}
// Update is called once per frame
void Update()
{
Vector3 rightMoveVector = (Vector3.right * right_joystick.Horizontal + Vector3.forward * right_joystick.Vertical);
if (rightMoveVector != Vector3.zero)
{
isShooting = true;
Debug.Log("masuk" + isShooting);
player.transform.rotation = Quaternion.LookRotation(rightMoveVector);
if (Time.time > nextFire)
{
StartCoroutine(fire());
nextFire = Time.time + fireRate;
}
}
else // no rightstick movement detected, will rotate towards player moveVector
{
isShooting = false;
}
}
public IEnumerator fire()
{
Instantiate(Bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.09f);
Instantiate(Bullet, transform.position, transform.rotation);
yield return new WaitForSeconds(0.09f);
Instantiate(Bullet, transform.position, transform.rotation);
}
}
I'm sorry for the late reply, actually i remembered replying to this. anyway you did helped me back then. thanks for the help !