- Home /
Question by
JiveViking · Jul 01, 2020 at 05:30 AM ·
vector3angleangleaxis
Shooting Direction with UI joystick
I'm a beginner to coding and I've downloaded a free UI joystick asset and I'm trying to use it to control the direction that my player is shooting (Top down view) It works well when used for movement, but with shooting the issue is that on lower sensitivities the bullets velocities vary. Basically I need to propel a projectile at the angle of the joystick at a consistent force
Here is the code
Vector3 shoot;
public VariableJoystick shootJoystick;
void update()
{
shoot = Vector2.right * shootJoystick.Horizontal + Vector2.up * shootJoystick.Vertical;
if(shootJoystick.Vertical != 0 || shootJoystick.Horizontal != 0)
{
joyAttack();
isShooting = true;
}
if (shootJoystick.Vertical == 0 && shootJoystick.Horizontal == 0)
isShooting = false;
}
void joyAttack()
{
GameObject newbullet = Instantiate(bullet, transform.position, transform.rotation);
newbullet.GetComponent<Rigidbody2D>().AddForce(shoot * shotpower);
}
Comment
Best Answer
Answer by JiveViking · Jul 01, 2020 at 08:37 AM
I changed the code a bit and I got a result that worked. I'm not sure if this is very efficient
void update(){
shotdir = Quaternion.AngleAxis(sAngle, Vector3.forward) * Vector3.right;
if(isShooting)
sAngle = Mathf.Atan2(shootJoystick.Vertical, shootJoystick.Horizontal) * Mathf.Rad2Deg;
}
void joyAttack(){
newbullet.GetComponent<Rigidbody2D>().AddForce( shotdir * shotpower);
}