- Home /
When Colliding With Animation, Player Re-spawns And Keeps Moving.
I am making a game for my ITS Class. I have made an Animation which is meant to be a "Moving Wall" in which if the "Player" collides with it, the "Player" re-spawns at the start of the Map.
The "Player collides and re-spawns but when it re-spawns the "Player" keeps moving forward and i can't stop it.
I have tried everything i know (Which is not much as i am relatively new to Unity :P) and searched the Forums but have not found an answer.
My thanks to anyone who can help me with my problem =).
*Edit*
Player Controller
var speed = 500.0;
var rotateSpeed = 1.0;
var bulletPrefab:Transform;
var bulletSpeed = 20000.0;
var bullet1Prefab:Transform;
var bullet1Speed = 20000.0;
private var dead = false;
//function OnControllerColliderHit(hit : ControllerColliderHit)
function OnTriggerEnter (hit:Collider)
{
if(hit.gameObject.tag == "Fallout")
{
dead = true;
Application.LoadLevel (1);
}
if(hit.gameObject.tag == "EnemyBullet")
{
Destroy (hit.gameObject);
dead = true;
//subtract life here
HealthControl.LIVES -= 1;
}
if(hit.gameObject.tag == "AniWall")
{
dead = true;
//subtract life here
HealthControl.LIVES -= 1;
}
}
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around Y - Axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Fire1"))
{
var bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity);
var bullet1 = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint1").transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
bullet1.rigidbody.AddForce(transform.forward * bullet1Speed);
}
}
function LateUpdate()
{
if(dead)
{
transform.position = Vector3(-991.336,46.12195,-775.5392);
gameObject.Find("Main Camera").transform.position = Vector3(10,4,-20);
dead = false;
}
}
@script RequireComponent(CharacterController)
There must be something in either your respawn or your playermovement code that causes this behaviour. Post it and maybe we can help.
Sorry about the formatting, i am not sure how to type the code here properly :P.
Answer by Joshua · Jul 08, 2011 at 11:11 AM
Don't worry about the formatting, I fixed it.
When he collider with the fallout you set 'dead' to true, and then load up scene( 1 ). All of the other code won't get run, because every object in your scene will be destroyed and replaced by the new scene. If the player then 'keeps moving forward', that is because that's the way you coded it in your new scene - there's nothing in this script that could cause that because this script gets destroyed upon levelload, and a new instanstance is created for the player in that new scene.
If you want this script to 'survive' scenechanges add DontDestroyOnLoad() to it.
If you want the lateUpdate() if( dead ) to be run, don't change the scene until it has.
Thank you for your reply and the formatting. I will test this now. $$anonymous$$any thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612135141im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to let the player collide with animated objects? 0 Answers
Trail of color behind the player 2D c# 1 Answer
Idle Animations, Player Walking 0 Answers
How to move player after animation 2 Answers