How to get a bullet to fire in the direction of the gun in mobile?

Updated on April 14, 2017 in [A] 2D
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
0 on April 14, 2017

Hello

I am working in unity with c#.I am working on a 2D platformer for mobile. The way I currently have it setup is one joystick that moves the charatcer makes the charatcer arm rotate and make the charatcer shoot the gun when pressed. Which all workd fine. But the problem I am having is the bullets from the gun are not going in the correct direction. Currently the bullets go to the rigtht no matter where I point the gun. I have tried in the raycats to use transform.right and transform.forward. All end up with the same result. the firepoint for the gun is an empty game object and the firepoint in the script is an tranfrom. I tried changing it to an GameObject in the script but I got the same results. If someone could please help it would be grealty appreciated!!!

Here is the shoot function with the raycast.
public void Shoot ()
{
Vector3 firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y, 0f);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, firePoint.transform.right, 100, whatToHit, 0f);

Debug.DrawLine(firePointPosition, (firePointPosition)*100, Color.cyan);
if (hit.collider != null)
{
Debug.DrawLine(firePointPosition, hit.point, Color.red);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if (enemy != null)
{
enemy.DamageEnemy(Damage);
//Debug.Log(“We hit ” + hit.collider.name + “and did ” + Damage + “damage”);
}
}

if (Time.time >= timeToSpawnEffect)
{
Vector3 hitPos;
Vector3 hitNormal;

if (hit.collider == null)
{
hitPos = (firePointPosition) * 30;
hitNormal = new Vector3(9999, 9999, 9999);
}
else
{
hitPos = hit.point;
hitNormal = hit.normal;
}

Effect(hitPos, hitNormal);
timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
}
}

  • Liked by
Reply