我想要做的就是当我的角色离开一个场景然后重新进入它离开它所在的位置时这样做。为了做到这一点,我想我可以从一个脚本中保存一个变量来保存和改变离开位置的角色,然后将其设置为进入场景时的角色。
我想使用当前玩家离开房间时获得的当前玩家位置。
using UnityEngine;
using System.Collections;
public class HomeBaseLevelSwitchOutside : MonoBehaviour {
public static Vector3 playerPos = GameObject.Find("Player").transform.position;
public GameObject Interaction;
void Start()
{
Interaction.SetActive (false);
}
void OnTriggerStay2D (Collider2D col)
{
Interaction.SetActive (true);
if (Input.GetKeyDown (KeyCode.E))
Application.LoadLevel (5);
HomeBaseLevelSwitchOutside.playerPos = GameObject.Find("Player").transform.position;
}
void OnTriggerExit2D(Collider2D col)
{
Interaction.SetActive (false);
}
}
然后将我的玩家设置为该位置
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
//floats
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
//Bools
public bool grounded;
public bool canDoubleJump;
//Stats
public int curHealth;
public int maxHealth = 100;
//References
private Rigidbody2D rb2d;
private Animator anim;
void Start ()
{
Player.transform.position = HomeBaseLevelSwitchOutside.playerPos;
rb2d = gameObject.GetComponent<Rigidbody2D> ();
anim = gameObject.GetComponent<Animator>();
//transform.position = Player.playerPos;
curHealth = maxHealth;
}
void Update ()
{
anim.SetBool ("Grounded", grounded);
anim.SetFloat ("Speed", Mathf.Abs (rb2d.velocity.x));
if (Input.GetAxis ("Horizontal") < -0.1f)
{
transform.localScale = new Vector3(-4, 4, 4);
}
if (Input.GetAxis ("Horizontal") > 0.1f)
{
transform.localScale = new Vector3(4, 4, 4);
}
if (Input.GetButtonDown ("Jump"))
{
if(grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
canDoubleJump = true;
}
else
{
if(canDoubleJump)
{
canDoubleJump = false;
rb2d.velocity = new Vector2 (rb2d.velocity.x,0);
rb2d.AddForce(Vector2.up * jumpPower);
}
}
}
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
if(curHealth <= 0){
Die ();
}
}
void FixedUpdate()
{
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.85f;
float h = Input.GetAxis ("Horizontal");
if(grounded)
{
rb2d.velocity = easeVelocity;
}
rb2d.AddForce ((Vector2.right * speed) * h);
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
void Die(){
//Restart
Application.LoadLevel (Application.loadedLevel);
}
public void Damage(int dmg){
curHealth -= dmg;
gameObject.GetComponent<Animation> ().Play ("redflash");
}
public IEnumerator Knockback(float knockdur, float knockpwr, Vector3 knockdir){
float timer = 0;
while (knockdur > timer) {
timer+=Time.deltaTime;
rb2d.AddForce(new Vector3(knockdir.x * -100, knockdir.y * knockpwr, transform.position.z));
}
yield return 0;
}
}
答案 0 :(得分:1)
播放器的Start方法中的第一行:
更改
Player.transform.position = HomeBaseLevelSwitchOutside.playerPos;
到
transform.position = HomeBaseLevelSwitchOutside.playerPos;
(您正在访问MonoBehaviour中的Transform,而不是Player类上的静态成员)