using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
// Private Variables
private float speed = 60.0f;
private float turnSpeed = 60.0f;
private float horizontalInput;
private float forwardInput;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// This is where we get player input
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
//move the vehicle forward
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
// We turn the vehicle
transform.Rotate(Vector3.up, Time.deltaTime * turnSpeed * horizontalInput);
}
}
4 Answers
4You need (at least) 2 colliders.
One for the player, one for the enemy (or object that should end the game).
You enable isTrigger on the enemy collider and give the enemy object the tag Enemy.
Then you use this in a script on the player:
void OnTriggerEnter(collider other)
{
if (other.gameObject.CompareTag("Enemy"))
{
// End the game
/* You can use */ Application.Quit(); /* or add a game over effect */
}
}
Ohh, Thank you but does it need rigid body for that cause the moment I apply that the objects start to fall into the void
So first of all:
At least one of the colliding objects needs to have a rigidbody.
Second of all:
Setting the enemy to isTrigger makes the collider only count as a trigger, thus not colliding with anything (including the ground) (it only collides as a trigger).
So yeah, objects with an isTrigger collider and a Rigidbody will fall trough the map (unless you have a second one with isTrigger disabled).
I would recommend unchecking the isTrigger box and using
void OnCollisionEnter(Collision collision)
for non static gameObjects with Rigidbodies (like your enemies I guess).
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
// Game Over screen
}
}
I use isTrigger for stuff like finishLines or specific areas, that shouldnt collide with the player but should do something if the player enters that specific area.
Kenan is right.
If you want the enemy to move, leave isTrigger disabled and use
void OnCollisionEnter (Collision collision)
If the object to end the game is something like a trap, wall or anything static (not moving), you can enable isTrigger, NOT use a rigidbody (as it doesn’t need one) and use
void OnTriggerEnter (Collider collider)