- Home /
How can I load a scene when the enemy hits the player?
Here's my script. Is there a way to start a new scene start as soon as the enemy hits the player?
var target : Transform;
var lookAtDistance = 20.0;
var attackRange = 15.0;
var moveSpeed = 20.0;
var damping = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.white;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.white;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
Is there a way to make a new scene start as soon as the enemy hits the player?
Answer by Graham-Dunnett · Jul 17, 2013 at 09:01 PM
Yes. Use collision detection to determine when the enemy hits the player, and use Application.loadLevel
to change scene.
Okay. How does collision detection work? Do I need a script or is it a component?
Answer by Ejpj123 · Apr 02, 2016 at 01:00 AM
@gsaccone101 Collision Detection is a Component. Add the componnet on what you want to not go through. I suggest, for 2D OR 3D, use the component Mesh Collider.
There is no Collision Detection Component. What you are referring to are Colliders.
Using the $$anonymous$$esh Collider for every 2D or 3D collision is not a very good suggestion, because depending on the cmesh complexity, they have way more impact on performance than standard primitive collider shapes like Box, Sphere, Capsule, etc..
Answer by DroidifyDevs · Apr 02, 2016 at 01:56 AM
Just use OnTriggerEnter to detect the collision, then use Application.LoadLevel to load another scene. Read the docs for those 2, they are basics of Unity and very easy but essential to use.
Answer by TX Manager · Apr 02, 2016 at 10:35 PM
It's easy follow this steps :-
void OnCollisionEneter (collision other)
{
if (other.colider.tag == "Enemy Shot")
{
SceneManager.LoadScene ("Scene Name Or Index");
}
}
don't forget to write using UnityEngine.SceneManagement;
in the up of the script if you are using unity 5.3 if not, use Application.LoadLevel ("Scene Name Or Index");
Answer by TCROC · Apr 02, 2016 at 10:37 PM
var target : Transform;
var lookAtDistance = 20.0;
var attackRange = 15.0;
var moveSpeed = 20.0;
var damping = 6.0;
private var isItAttacking = false;
private int currentLevel;
function Start () {
// This is the current scene you are in and have built in build settings
currentLevel = Application.loadedLevel;
}
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.white;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.white;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
// This will reload the scene you are currently on. If you would like to load the next scene you would say Application.LoadLevel (currentLevel + 1);
Application.LoadLevel (currentLevel);
}
"private int currentLevel" should be "var currentLevel". $$anonymous$$y bad I'm used to C# :)
Your answer
Follow this Question
Related Questions
enemy targeting player in different scene 1 Answer
Kill the Player? 1 Answer
Enemy targeting player in different scene 1 Answer
Collision Detection Issue with SetActive(false) not working 2 Answers
auto select enemys 3 Answers