How do i make game objects return to their original position after collision?
I'm making a game where you are an alien (made by one square and three other stretched squares for the legs) that haves to reach an objective while dodging some circles that make you return at your original position.
I'm having a problem making you return at your original position because the square touches the circles an returns to original position while the legs don't.
Here's my code so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeathCollision : MonoBehaviour
{
Vector2 originalPos;
void Start()
{
originalPos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.Contains("Death"))
{
gameObject.transform.position = originalPos;
}
}
} Can you please tell me a way to mention legs so that i can return them too to the original position?
Answer by laumil99 · Aug 09, 2021 at 03:01 PM
try a transform instead of Vector2 and in your start ( u can also do it in a Awake) try originalPos.position.x and same for y.
public class PlayerManager : MonoBehaviour {
[SerializeField] private GameObject _playerPrefab = null;
[SerializeField] private Transform spawnPointPlayer = null;
private void Awake()
{
GameObject player = Instantiate(_playerPrefab, spawnPointPlayer.position, spawnPointPlayer.rotation);
}
}
this is what I do for a FPS game, and i drag and drop a spawnPoint that i create as a emptyGame object in my hierarchy.
Answer by junk_rat_angel · Aug 11, 2021 at 03:20 AM
you can also use parenting and then when you reset the position of the parent the children will follow. from the sound of it this might require you to change the way your legs are working though. you can also just add a public declaration of them as variables and drag and drop them from the editor by typing public GameObject leg1; public GameObject left2; etc.. youll want to store the original position for each of them then to maintain their respective offsets so that they dont all return to the same spot.
Your answer
Follow this Question
Related Questions
Soccer game, more realistic net 1 Answer
How To Make A Character Stop At A Block? 0 Answers
Scripting trouble, Float on Collision/collision open gate 2 Answers
How to make a Compact-Disc shaped collider object with an empty hole inside? (2D, not a 3D torus) 0 Answers
Count objects inside a dynamic area? 0 Answers