- Home /
Gun aim off center
Hello,
I have this gun script that rotates and aims my gun so that I dont have to create 100's of aiming in animations, however there is one bug. If you rotate whilst not aiming in then aim in, the gun does not aim down the centre of the screen. This is a huge issue and one that has been causing me troubles for a couple of days now. If anyone could help that would be great, I wont upload pictures of the issue now but if you need them feel free to ask. Here is the script that deals with the aiming in and smooth rotation:
using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour {
public Transform playerTransform;
public float RotationSpeed;
public GameObject playerCamera;
//Targets
private float targetXRotation;
private float targetYRotation;
private float targetXRotationV;
private float targetYRotationV;
//Aiming
public float holdHeight = -0.1f;
public float holdSide = 0.1f;
public float racioHipHold = 1f;
public float hipToAimSpeed = 0.1f;
private float racioHipHoldV;
void Start(){
}
void Update () {
if(Input.GetButton("Fire2")){
playerCamera.GetComponent<MouseLook>().sensitivityX = 2f;
playerCamera.transform.parent.GetComponent<MouseLook>().sensitivityX = 2f;
playerCamera.GetComponent<MouseLook>().sensitivityY = 2f;
playerCamera.transform.parent.GetComponent<MouseLook>().sensitivityY = 2f;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, ref racioHipHoldV, hipToAimSpeed);
}
if(!Input.GetButton("Fire2")){
playerCamera.GetComponent<MouseLook>().sensitivityX = 7f;
playerCamera.transform.parent.GetComponent<MouseLook>().sensitivityX = 7f;
playerCamera.GetComponent<MouseLook>().sensitivityY = 7f;
playerCamera.transform.parent.GetComponent<MouseLook>().sensitivityY = 7f;
racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, ref racioHipHoldV, hipToAimSpeed);
}
transform.position = playerCamera.transform.position + (Quaternion.Euler(0, targetYRotation,0) * new Vector3(holdSide * racioHipHold,holdHeight * racioHipHold,0));
targetXRotation = Mathf.SmoothDamp(targetXRotation, -playerCamera.GetComponent<MouseLook>().rotationY, ref targetXRotationV, RotationSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation, playerCamera.GetComponent<MouseLook>().RotationX, ref targetYRotationV, RotationSpeed);
transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}
}
Do you mean that the ai$$anonymous$$g lags too much behind where the camera is looking ? or do you mean because the gun is offset from the center of the camera, the bullets are off to the side, and dont hit the spot that is the center of the screen (where the gun sights are) ? Problem 1, just reduce your lag, or position your gun and camera 'after' the input update... and in the correct order. (or just parent the gun onto the camera) for problem 2. its an age old FPS dilemma. Trick is to spawn your bullets from the camera position... not from the gun. (but you get other side effects also doing this)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# scriping help 0 Answers
MonoDevelop 4.0.1 code completion doesn't work 2 Answers
C# Script is affecting multiple GameObjects but I would like it to only affect one of them... 1 Answer
Multiple Cars not working 1 Answer