I need my PlayerController (which is generated dynamically) to have it's position updated each frame, so it is always centered between the center line and the anchor end: https://goo.gl/G9ZZTp
Here is the code I am using right now:
using UnityEngine; using System.Collections;
public class CenterPlayer : MonoBehaviour {
private GameObject origin;
private GameObject destination;
private GameObject anchor;
// Use this for initialization
void Start () {
origin = GameObject.Find("Origin");
Debug.Log(origin);
destination = GameObject.Find("Destination");
anchor = GameObject.Find("Left");
}
// Update is called once per frame
void Update () {
float targetXposition = (((origin.transform.position.x + destination.transform.position.x) / 2f) + anchor.transform.position.x) / 2f;
Vector3 position = transform.position;
this.transform.position = new Vector3(targetXposition, position.y, position.y);
}
I have this script attached to the playerController. It doesn't work and I can't figure out why. The line will move sometimes and I need the player to be always centered between the line and the anchor.