- Home /
Question by
YT5h4dow · Sep 12, 2018 at 02:59 AM ·
transform.positionrespawn
tranform.position always leads to 0, 0
im trying to make a respawn point for my player after they die but when i do transform.position = new Vector2(x, y) it always leads back to 0, 0
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float RespawnX = -13;
public float RespawnY = -5;
public float speed;
public float jumpForce;
private float inputHorizontal;
private float inputVertical;
public float health;
public float MaxHealth;
public Image healthBar;
public Text healthAmount;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
public float distance;
public LayerMask whatIsLadder;
public bool isClimbing;
public GameObject BloodParticlesPlayer;
//private Animator anim;
private void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
//anim = GetComponent<Animator>();
}
private void FixedUpdate()
{
if (health == 0)
{
//temporary death
Respawn();
}
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
inputHorizontal = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(inputHorizontal * speed, rb.velocity.y);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.up, distance, whatIsLadder);
Debug.DrawRay(transform.position, Vector2.up, Color.green);
if (facingRight == false && inputHorizontal > 0)
{
flip();
}
else if (facingRight == true && inputHorizontal < 0)
{
flip();
}
if (hitInfo.collider != null)
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
isClimbing = true;
}
}
else
{
isClimbing = false;
}
if (isClimbing == true)
{
inputVertical = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector2(rb.velocity.x, inputVertical * speed);
rb.gravityScale = 0;
}
else
{
rb.gravityScale = 1;
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
SetStatBars();
}
void flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
public void TakeDamage(int damage)
{
Instantiate(BloodParticlesPlayer, transform.position, Quaternion.identity);
health -= damage;
Debug.Log("player took damage");
}
public void SetStatBars()
{
healthBar.fillAmount = health / MaxHealth;
healthAmount.text = health.ToString() + "/" + MaxHealth.ToString();
}
public void Respawn()
{
transform.position = new Vector2(RespawnX, RespawnY);
health = MaxHealth;
}
}
Comment
Best Answer
Answer by Eno-Khaon · Sep 12, 2018 at 04:03 AM
The variables are declared as public, so they're serialized in the editor. Are RespawnX and RespawnY set to non-zero variables in the editor for the object they're attached to?
by setting the variables in the start function the variables are set when the project runs and everything works fine thanks for the help