- Home /
How can I LERP the X position to the X position of the player
with my code i've only gotten the game object to to go directly to the X position of the player - though it moves in a jagged manner
using UnityEngine;
using System.Collections;
public class SetPositonToPlayerX : MonoBehaviour {
private Transform myTarget; // Using it to grab the player's position
private float playerXPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Looks for the "the player" + the transform
if (myTarget == null)
{
myTarget = GameObject.Find("The Player").transform;
Debug.Log("Yo dawg Where'd the Player Ship go!?");
return;// if The TARGET iS NOT FOUND - try again in the next frame.
}
///////////////////////////////////////////////////////////////////////////////
if (myTarget != null) {
playerXPosition = myTarget.position.x;
transform.position = new Vector2(playerXPosition, transform.position.y);
}
}
}
try running it under FixedUpdate ins$$anonymous$$d of update
Answer by Berge · Apr 19, 2015 at 03:18 PM
See the documentation on Vector2.Lerp. http://docs.unity3d.com/ScriptReference/Vector2.Lerp.html
transform.position = Vector2.Lerp(transform.position, new Vector2(playerXPosition, transform.position.y), 0.2);
the gameobject doesnt stay in its zposition + moves to the exact X position (moving all jagged-like)
Answer by Salman_Shh · Apr 22, 2015 at 02:12 AM
using UnityEngine;
using System.Collections;
public class SetPositonToPlayerX : MonoBehaviour{
private float x;
private float y;
private float z;
Vector3 pos;
private Transform myTarget; // Using it to grab the player's position
private float playerXPosition;
public float Smoothing = 0.2f;
// Use this for initialization
// Update is called once per frame
void Update()
{
//the game object's X Position
//float goXPosition = transform.position.y;
// Looks for the "the player" + the transform
if (myTarget == null)
{
myTarget = GameObject.Find("The Player").transform;
Debug.Log("Yo dawg Where'd the Player Ship go!?");
return;// if The TARGET iS NOT FOUND - try again in the next frame.
}
// if the player target is found
if (myTarget != null)
{
//Updates the Player X Position variable to the target's X position
playerXPosition = myTarget.position.x;
setToPositionX();
}
}
void setToPositionX(){
//gameObject.transform.position
//transform.position = new Vector3(Vector3.Lerp(transform.position.x, playerXPosition, Time.deltaTime), transform.position.y), transform.position.z);
//Sets the x position float with lerp.
x = Mathf.Lerp(playerXPosition, transform.position.y, Smoothing);
//Sets the Y Position to gameobjects's y Position
y = transform.position.y;
//sets the Z Position to the gameobject's Z Position.
z = gameObject.transform.position.z;
//Puts all the positions together
pos = new Vector3(x, y, z);
//Sets the objects position to the new position from "pos"
transform.position = pos;
}
}
I found out my own solution - this worked for me I'm able to change the rate in which the gameobject moves towards the target x position by the "smoothing" variable.