Question by
Joni2804 · Oct 14, 2020 at 03:50 PM ·
weaponaimingaimdownsights
Gun model not corectly pointing at center of screen when using ironsights,Gun model not correctly pointing at the middle of the screen when aiming
I made a Gun Model for an MP40, now when I aim it using the animator, and I look around, the Ironsight doesnt correctly follow the Crosshair im Using, only when aiming straight forward, up, or down! I tried putting the weapon in a new position, but that didnt work either, I would appreciate some help, and thanks in advance!
Heres my Scipt for Automatic Weapons, and Pictures of what I mean, if thats of any use...
using UnityEngine;
using System.Collections;
using TMPro;
public class GunScript_A : MonoBehaviour
{
//----------------------------------------------------------------
public float damage = 10f;
public float range = 100f;
public float impactForce = 30f;
public float fireRate = 15f;
public float maxAmmo = 10;
private float currentAmmo;
public float reloadTime = 3f;
private bool isReloading = false;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public TextMeshProUGUI ammunitionDisplay;
private float nextTimeToFire = 0;
public Animator animator;
//-------------------------------------------------------------------
void Start()
{
currentAmmo = maxAmmo;
AmmoDisplay();
}
void OnEnable()
{
isReloading = false;
animator.SetBool("Reloading", false);
}
// Update is called once per frame
void Update()
{
if(isReloading)
{
return;
}
if(currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
//Shoot
if(Input.GetMouseButton(0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f/fireRate;
Shoot();
}
//Reload
if (Input.GetKeyDown(KeyCode.R) && currentAmmo < maxAmmo)
{
StartCoroutine(Reload());
return;
}
//Amining
if(Input.GetButton("Fire2") && isReloading == false)
{
animator.SetBool("Aiming", true);
}
else
{
animator.SetBool("Aiming", false);
}
if (ammunitionDisplay != null)
{
AmmoDisplay();
}
}
IEnumerator Reload()
{
animator.SetBool("Aiming", false);
isReloading = true;
Debug.Log("Reloading...");
animator.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("Reloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
muzzleFlash.Play();
currentAmmo--;
RaycastHit hit;
if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
//Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if(target != null)
{
target.TakeDamage(damage);
}
if(hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}
void AmmoDisplay()
{
ammunitionDisplay.SetText(currentAmmo + " / " + maxAmmo);
}
}
2020-10-14.png
(348.5 kB)
2020-10-14-1.png
(392.8 kB)
Comment
Your answer
Follow this Question
Related Questions
Calculate offset to other object for aim down sight system? 0 Answers
Smooth aiming script don't work 0 Answers
Store and access weapons from list C# 0 Answers
2D Boomerang Style Weapon 1 Answer
how to make bullet explode on impact? 0 Answers