Quantcast
Channel: Questions in topic: "follow"
Viewing all articles
Browse latest Browse all 536

Having a friendly character follow player

$
0
0
Hello! I'm trying to get an array of characters to follow the Player's exact path. The code I have works but the characters catch up if the player collides with something (I believe the storedPositions list is generating vector3's that are really close together). In addition, when switching scenes, the characters remain on the followDistance frame and are separated from the player briefly. I've tried alternatives like using moveToward/larp but I'd rather they follow a strict path similar to the way Earthobound and Mother 3 handle it. Does anyone know an alternative or good resource I can look at to figure this out? I'm still very new to programming. using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowCharacter : MonoBehaviour { public int followDistance; public Vector3 lastMove; private List storedPositions; private GameObject player; private Vector2 moveInput; private static bool buddyExists; void Awake() { storedPositions = new List(); //create a blank list player = GameObject.Find("Character"); if (!player) { Debug.Log("The FollowingMe gameobject was not set"); } if (followDistance == 0) { Debug.Log("Please set distance higher then 0"); } } void Start() { if (!buddyExists) { buddyExists = true; DontDestroyOnLoad(transform.gameObject); } else { Destroy(gameObject); } } void Update() { if (storedPositions.Count == 0) { Debug.Log("blank list"); storedPositions.Add(player.transform.position); //store the players currect position return; } else if (storedPositions[storedPositions.Count - 1] != player.transform.position) { //Debug.Log("Add to list"); storedPositions.Add(player.transform.position); //store the position every frame Debug.Log("number is " + storedPositions.Count); } if (storedPositions.Count > followDistance) { transform.position = storedPositions[0]; //move storedPositions.RemoveAt(0); //delete the position that player just move to } // foreach (Vector3 thing in storedPositions) // { // Debug.Log(thing); // } } }

Viewing all articles
Browse latest Browse all 536

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>