- Home /
Vector3.Lerp move weapon up to players face
Why when I move a child object (a weapon) using Vector3.Lerp does it not return to its original position when walking around with the below code? All I'm trying to do is move my weapon up close to the players face, I also have a script that zooms the camera in so it looks like you are zooming into the target.
Any help would be appreciated, thanks.
#pragma strict
private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
function Awake(){
globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
}
function Start () {
}
function Update () {
if (Input.GetButtonDown("Zoom In")){
StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + Vector3(-0.25, 0.05, 0), 1));
}
if (Input.GetButtonDown("Zoom Out")){
StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + Vector3(0.25, -0.05, 0), 1));
}
}
function MoveToPosition(newPosition : Vector3, time : float)
{
var elapsedTime : float = 0;
var startingPos = globalVars.GetComponent(GlobalVars).currentWeapon.transform.position;
while (elapsedTime < time)
{
globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
}
The code seems right. Try to check if the Zoom Out function is being called, and if it is try to make the camera follow the player without being a child, just to make sure that the parent orientation isn't messing up with the positions.
But if I just use Translate it goes and returns where expected every time while walking around, but I wanted it to animate to the new position not just appear ..
#pragma strict
private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
function Awake(){
globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
}
function Start () {
}
function Update () {
if (Input.GetButtonDown("Zoom In")){
globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(-0.25,0.05,0));
}
if (Input.GetButtonDown("Zoom Out")){
globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(0.25,-0.05,0));
}
}
Well, if your post up ahead is working, then now try to change the lines:
globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(-0.25,0.05,0));
globalVars.GetComponent(GlobalVars).currentWeapon.transform.Translate(Vector3(0.25,-0.05,0));
to this:
globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position, new Vector3(-0.25,0.05,0), 1 );
globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position, new Vector3(0.25,-0.05,0), 1 );
BLarentis thanks for that but it just jumps to the position like my code above.
Try to change the time 1 to 0.2. Or in last case try to use ITween: http://itween.pixelplacement.com/index.php
Answer by Griffo · Aug 14, 2012 at 08:58 AM
If anyone else gets stuck with something like the problem I had here is the answer ..
#pragma strict
private var globalVars : GameObject; // var for the GameObject with the GlobalVars on
function Awake(){
globalVars = GameObject.Find("Global Vars"); // Find the gameObject with the GlobalVars attached to it
}
function Start () {
}
function Update () {
if (Input.GetButtonDown("Zoom In")){
var zoomInOffset = Vector3(-0.25, 0.05, 0);
var zoomInWorldPosition = transform.TransformDirection( zoomInOffset );
StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + zoomInWorldPosition, 0.5));
}
if (Input.GetButtonDown("Zoom Out")){
var zoomOutOffset = Vector3(0.25, -0.05, 0);
var zoomOutWorldPosition = transform.TransformDirection( zoomOutOffset );
StartCoroutine(MoveToPosition(globalVars.GetComponent(GlobalVars).currentWeapon.transform.position + zoomOutWorldPosition, 0.5));
}
}
function MoveToPosition(newPosition : Vector3, time : float){
var elapsedTime : float = 0;
var startingPos = globalVars.GetComponent(GlobalVars).currentWeapon.transform.position;
while (elapsedTime < time){
globalVars.GetComponent(GlobalVars).currentWeapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
}
Answer by Dragonlance · Aug 13, 2012 at 06:07 PM
If the wapon is a child of the player, maybe you want to move the localPosition instead?