- Home /
RayCast2D direction is always showing origin point(0,0,0) on game and ignores direction Vector2D.(Unity 2017.4.7.f1)
Im working on a game which includes a soldier defending a barracks and he carries a gun. He is rotating arm on z-axis with joystick and other stuffs (muzzle flash, line renderer,rifles, etc.). And when it came to line RayCast2D some worked. RayCast2D origin is working but direction vector is not working. I attain both value and direction from the center point. I set the direction point at 1 unit from the center point on the sceen.Direction point is not working on the script. It is always showing the same place which is origin point on the Scene.
public class ShootingRifle : MonoBehaviour
{
public static float effectSpawnRateRifle = 10f;
public static float fireRateRifle = 0;
public static float DamageRifle = 10;
public static LayerMask whatToHitWithRifle;
public Transform BulletTrailRiflePrefab;
public Transform MuzzleFlashRiflePrefab;
[SerializeField]
private Transform firePointRifle;
[SerializeField]
private Transform directionPointRifle;
float fireRateRifle2 = 0;
float effectRateRifle2 = 0;
//Gun myGun;
private void Start()
{
//myGun = GetComponent<Gun>();
//float holding = ;
}
void Update()
{
DamageRifle = Rifle.weaponDamage;
fireRateRifle = Rifle.rateofFire;
whatToHitWithRifle = Rifle.WhatWeHit;
effectSpawnRateRifle = Rifle.effectSpawnRate;
if (Time.time > fireRateRifle2 && CrossPlatformInputManager.GetButton("Hold"))
{
fireRateRifle2 = Time.time + fireRateRifle;
Shoot();
}
}
void Shoot()
{
Vector2 firePointPosition = new Vector2(firePointRifle.position.x, firePointRifle.position.y);
Vector2 directionPoint = new Vector2(directionRifle.position.x, directionRifle.position.y);
//Ignore directionPoint here
RaycastHit2D hit = Physics2D.Raycast(firePointPosition,directionPoint, 100, whatToHitWithRifle);
Debug.DrawLine(firePointPosition, hit.point, Color.red);
//Debug.Log("We hit " + hit.collider.name + "And did" + Damage + "damage");
if (hit.collider != null)
{
}
if (Time.time > effectRateRifle2 && CrossPlatformInputManager.GetButton("Hold"))
{
effectRateRifle2 = Time.time + effectSpawnRateRifle;
Effect();
}
}
void Effect()
{
Instantiate(BulletTrailRiflePrefab, firePointRifle.position, firePointRifle.rotation * Quaternion.Euler(0f, 0f, 1f));
Transform clone = Instantiate(MuzzleFlashRiflePrefab, firePointRifle.position, firePointRifle.rotation * Quaternion.Euler(0f, 0f, 1f)) as Transform;
//clone.parent = firePoint;
//float size = Random.Range(1f, 1.4f);
//clone.localScale = new Vector3(size, size, size);
Destroy(clone.gameObject, 0.02f);
}
}
Your answer
Follow this Question
Related Questions
Cinemachine camera rotating with player 2D 1 Answer
How To Fix My RayCastHit2D in Unity 4.3.4? 0 Answers
Flip over an object (smooth transition) 3 Answers
Start Coroutine after other has finished 4 Answers
OnTriggerEnter2D with multiple objects 0 Answers