- Home /
I want to lerp a gun from positionA to positionB in relation to the main camera
I'm making an aiming down the sight script and i'v made my gun a child of the main camera,i want to lerp it in relation to the main camera when the right mouse button is being held, and when its not,lerp it back to positionA.Heres what i'v got so far, BTW it's in c#:
using UnityEngine;
using System.Collections;
public class TheProperAimDownSight : MonoBehaviour{
bool lerp;
void Update (){
if(Input.GetMouseButtonDown(0))
{
lerp = true;
}
if(Input.GetMouseButtonUp(0))
{
lerp = false;
}
if(lerp)
transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime);
else
transform.position = Vector3.Lerp(positionB, positionA, Time.deltaTime);
}
}
Ins$$anonymous$$d of adding the gun to the camera, which complicates the coding, why not add a camera to the player?
This is your 7th question on this topic. Lots of the previous questions have been answered and accepted.
I have not been involved in the other questions, but if you want to move 'in relation to the main camera', and the objects are children, then you want to use Transform.localPosition.
robert....
http://answers.unity3d.com/questions/561146/how-do-i-move-my-gun-to-position-b.html
You answered one of them :D
I'm sorry for asking this many questions but I'm stuck, none of the answers really helped, also if i make the gun a child of the character, i can't look up down, left and right correctly.
Answer by Josh707 · Nov 05, 2013 at 06:29 PM
Instead of telling him/her how many times they have posted, why not just give a simple answer?
Your code is right, you just need to replace:
transform.position = Vector3.Lerp(positionA, positionB, Time.deltaTime);
With:
transform.localPosition = Vector3.Lerp(transform.localPosition, positionB, Time.deltaTime);
For both of the lerps. Using localPosition will have it move relative to it's parent (the camera) regardless of the parent rotating and moving, and if you want it to be able to smoothly go from any position inbetween to the target, it needs to lerp from it's current position. So if you let go before it is fully aimed, it won't snap to the aimed position and start moving back, it will be more like CoD per se.
you know what josh, you're my new favourite person, THAN$$anonymous$$ YOU, ive been stuck on that for a while, and we all got to start somewhere right?