- Home /
The question is answered, right answer was accepted
Player movements go wrong when respawning after collision with enemy
Hello Ladies & Gents Yes , before posting this i spent a day and half trying and looking for answers but the one I am looking for I did not find it.
I have player moving left and right using Horizontal axis input , while it continuously move forward with transform.Translate(0,Yvalue,0);
Everything goes fine till the player collides with an enemy or enemy bullet. It seems that the player kind of acquire the movements of the enemy or it start to rotate without input once it respawn. I followed some instructions found in the forums and after the collision the player start from the starting position I want but the movements are not the ones of before but all seem casual and independent from input , what I am doing wrong? thank you. EDIT : I have tried also to use individual key down but the result is baffling cause the player, only after the first respawn seems to be influenced by external forces which i do not understand Collision script :
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Lake")
{
deathAndLives.lives--;
StartCoroutine("Respawn");
}
}
public IEnumerator Respawn()
{
gameObject.GetComponent<Movement>().enabled = false;
gameObject.GetComponent<Renderer>().enabled = false;
gameObject.GetComponent<CapsuleCollider2D>().enabled = false;
var objectPOS = transform.position;
GameObject Explosion = Instantiate(deathVFX, objectPOS, Quaternion.identity);
Destroy(Explosion, 0.7f);
yield return new WaitForSeconds(1f);
transform.position = new Vector3(xstart, ystart, zstart);
gameObject.GetComponent<CapsuleCollider2D>().enabled = true;
gameObject.GetComponent<Renderer>().enabled = true;
gameObject.GetComponent<Movement>().enabled = true;
}
The movement script :
public class Movement : MonoBehaviour
{
[SerializeField] public float steering;
[SerializeField] public float speed;
void FixedUpdate()
{
Move();
}
private void Move()
{
float steeramount = Input.GetAxis("H") * steering;
transform.Rotate(0, 0, steeramount * Time.deltaTime);
transform.Translate(0, speed * Time.deltaTime, 0);
}
}
Do you have a rigidbody on your character? If so have you tried setting IsKinematic to true?
Answer by unity_418scarlet · Nov 13, 2021 at 01:45 PM
hello bdubbert , thank you, i did but after that there were no collisions between the player and the rest of the things. I just solve it ( it seems so) taking away these
gameObject.GetComponent().enabled = true; gameObject.GetComponent().enabled = true; gameObject.GetComponent().enabled = true;
and destroy the player and instantiate it after the death.
Follow this Question
Related Questions
Cannot move FPSController after respawning at a spawn point. 1 Answer
How to move a Game Object from a script not attached to it. 1 Answer
If trigger hit, spawn it 1 Answer
How to move multiple dynamic gameobjects with one moving platform which is controlled by the player 0 Answers
Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers