- Home /
How to move gun from middle of screen to right of screen?
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;
}
}
}
}
Answer by Pokedlg3 · Dec 19, 2020 at 05:27 PM
Just turn the weapon into a child object of the camera. In the hierarchy, take your weapon and drag it to your camera, so that you will turn the weapon into a child object of the camera and it will move according to a camera.
I already did that, I want to move it from the middle of the camera to the right of the camera when I stop ai$$anonymous$$g.
Well, I analyzed your script and try to add this:
if (gunAi$$anonymous$$g == false)
{
gun.transform.position = Vector3.Lerp(gun.transform.position, Vector3.right, Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
when float is less then zero, do action once 1 Answer
Weapon random movement 0 Answers
FPS - Attach a gun to hands and move arms when aiming up and down 1 Answer
Aiming Gun at Cursor 2 Answers
I Want To Shoot Bullets Correctly. 2 Answers