- Home /
Setting start point for player 2d
I am trying to set the player start point in my 2d rpg game when a player changes scenes. Currently with no script the player stays at the same x and y point which is not good. When I add in the below script the camera starts in the right area and the player can walk around but there is no character? It is almost as if the character is below the screen or invisible. I followed a video tutorial and it works for the poster but it does not work for me. I did update the script slightly to work with the current unity so maybe I caused an issue there. Thanks in advance for the help!
using System.Collections; using System.Collections.Generic; using UnityEngine; //This checks player position when loading into new levels or scenes
public class PlayerStartPoint : MonoBehaviour {
private PlayerControl thePlayer;
private CameraControl theCamera;
public Vector2 FaceDirrection;
// Use this for initialization
void Start () {
thePlayer = FindObjectOfType<thePlayer> ();
thePlayer.transform.position = transform.position;
thePlayer.LastMove = FaceDirrection;
theCamera = FindObjectOfType<theCamera> ();
theCamera.transform.position = new Vector3 (transform.position.x, transform.position.y, theCamera.transform.position.z);
}
// Update is called once per frame
void Update () {
}
}
Answer by jmgek · Feb 08, 2017 at 05:24 PM
Try to stay away from findobjectoftype, you especially don't need it in this case.
Its slow and it returns the first object it finds in the scene of type casted. Making it un extendable and a bad practice if your trying to learn how to program.
You should be assigning your player to your thePlayer.
[serializeFeild]
private PlayerControl thePlayer;
Then just drag your player control into the inspector. Also control your position before you spawn, what's the reason your spawning then trying to set the position after?
Answer by Walter_Hulsebos · Feb 08, 2017 at 04:57 PM
Maybe the parent object of your player is not enabled? Or you could replace FindObjectOfType<thePlayer>()
to FindObjectOfType<PlayerControl>()
Your answer
Follow this Question
Related Questions
Start points don't work when I switch from the title screen, to the playable scene 0 Answers
How would you make a 2D NPC walk from one point to another for a cutscene? 1 Answer
respawn all after player die or doing checkpoint 1 Answer
Add item to GUI inventory and drop him back in the scene with keys 1 Answer
2d top-down rpg direction check 1 Answer