Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Gun Aiming Script Almost Perfect

Discussion in 'Scripting' started by toastybatch565, Jul 4, 2014.

  1. toastybatch565

    toastybatch565

    Joined:
    Mar 14, 2014
    Posts:
    4
    I have come pretty far with this one script, and it's almost exactly as I want it, however there's one problem.
    The script is a sniper rifle scope aiming script and I have the camera field of view lerp into 20 from 100 very smoothly, I love it, but the gun stays floating where it spawns. I have the gun as a child to the main camera which is a child of the first person controller, and I have the script on the gun. When I first start, the gun stays in place, but it smoothly turns to face where I am facing. However, it will not follow my character, and when I press right click, it disappears. I think that the problem is because the locations of Vector3 aren't relative to the character controller, but I don't know how to make it so. Please help a n00b. ^^
    HERE IS THE SCRIPT

    public var smooth : float;

    private var newPosition : Vector3;
    private var newFov : float;


    function Awake ()
    {
    newPosition = transform.position;
    newFov = Camera.main.fieldOfView;
    }

    function Update ()
    {
    PositionChanging();
    FovChanging();
    }


    function PositionChanging ()
    {
    var positionA : Vector3 = new Vector3(0.5014648, -1.076523, 0.1868896);
    var positionB : Vector3 = new Vector3(-0.01593018, -0.7554626, -0.2791138);

    if(Input.GetButtonDown("Fire2"))
    newPosition = positionB;
    if(Input.GetButtonUp ("Fire2"))
    newPosition = positionA;

    transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);

    }


    function FovChanging ()
    {
    var fovA : float = 100;
    var fovB : float = 20;

    if (Input.GetButtonDown("Fire2"))
    newFov = fovB;
    if (Input.GetButtonUp("Fire2"))
    newFov = fovA;

    camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, newFov, smooth * Time.deltaTime);
    }
     
  2. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Why would you think it would do ANYTHING differently? You are changing the objects GLOBAL position to one of 2 points, meaning that object will ONLY be in one of those two points regardless of relation to its parents. If you want it to move like a normal child but still need those points as offsets make them adjust the LOCAL position and they will move accordingly.
     
  3. toastybatch565

    toastybatch565

    Joined:
    Mar 14, 2014
    Posts:
    4
    How would I make it local? transform.localPosition?
     
  4. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    Yeh. though if your weapon starts where you want it relative to the player/camera you are gonna want to return your local position to Vector3.zero and not the other.