So i’m making an FPS where time slows down when you aim. There’s an aim counter that automatically un-aims the gun when you’ve been aiming too long. For this, I’m moving the gun from the center of the screen (when you aim) to the right of the screen (when you’re not aiming). I don’t want to move the gun to a specific position, because it should follow the camera. I’ve been using transform.Translate, but it’s not working. i’m fairly new to unity, so any help would be appreciated.
this is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform gunTip;
public float speed = 8f;
public bool inRange = false;
public Camera camera;
public GameObject gun;
public float maxAim = 3f;
public float currentAim;
public float aimWait;
public bool gunAiming;
public bool gunRight;
public bool gunMoved;
// Start is called before the first frame update
void Start()
{
currentAim = maxAim;
gunAiming = false;
aimWait = 1f;
gunMoved = false;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
GameObject bulletObject = Instantiate(bulletPrefab);
bulletObject.transform.position = gunTip.transform.position;
bulletObject.transform.forward = gunTip.transform.forward * speed;
}
if(currentAim <= 0)
{
gunRight = true;
gunMoved = true;
}
else
{
gunRight = false;
gunMoved = false;
}
if(Input.GetKeyDown(KeyCode.Mouse1))
{
gun.transform.Translate(Vector3.right);
gunAiming = true;
}
if (Input.GetKeyUp(KeyCode.Mouse1))
{
gun.transform.Translate(Vector3.left);
gunAiming = false;
}
// StartCoroutine(Aim());
Aim();
if (Input.GetKey(KeyCode.Mouse1))
{
// Aim();
gunAiming = true;
}
else
{
// NoAim();
gunAiming = false;
}
}
void Aim()
{
if (gunAiming)
{
// gun.transform.Translate(Vector3.right);
if(!gunRight)
{
// gun.transform.Translate(Vector3.left);
Time.timeScale = 0.1f;
camera.fieldOfView = 45;
currentAim -= Time.deltaTime * 10;
}
if(gunRight)
{
if(gunMoved)
{
gun.transform.Translate(Vector3.left);
gunMoved = false;
}
if(!gunMoved)
{
Time.timeScale = 1f;
camera.fieldOfView = 60;
}
// yield return new WaitForSecondsRealtime(0.02f);
// gunRight = false;
}
}
if (!gunAiming)
{
Time.timeScale = 1f;
camera.fieldOfView = 60;
if (currentAim < maxAim)
{
currentAim += Time.deltaTime / 2;
}
}
}
}