using UnityEngine;
using System.Collections;
public class underWater : MonoBehaviour {
public float uwaterLevel=15f;
public Color normalColor;
public Color underWaterColor;
void Start () {
normalColor = new Color (0.5f,0.5f,0.5f,0.5f);
underWaterColor = new Color (0.22f,0.65f,0.77f,0.5f);
}
void Update () {
Debug.Log (uwaterLevel);
Debug.Log (transform.position.y);
if(Camera.main.transform.position.y<uwaterLevel){
Debug.Log("Underwater Update");
setUnderWater();
}else{
Debug.Log("Normal Update");
setNormal();
}
}
public void setUnderWater(){
Debug.Log("Under Water");
RenderSettings.fog = true;
RenderSettings.fogDensity = 0.05f;
RenderSettings.fogColor = underWaterColor;
}
public void setNormal(){
Debug.Log("Normal");
RenderSettings.fog = true;
RenderSettings.fog = false;
RenderSettings.fogDensity = 0.003f;
RenderSettings.fogColor = normalColor;
}
}
How can I detect if my character is under water? Water is above the ground(a lake between the hills) so I can't use negative Y-axis. I'm using FPSController from Unity Standard Assests.
Make a large Collider the size of the water. Make it a trigger. Make it's tag "Water"
public void OnTriggerEnter(Collider col)
{
if(col.tag == "Water")
{
// In water
}
}
public void OnTriggerStay(Collider col)
{
if(col.tag == "Water")
{
// In water
}
}
public void OnTriggerExit(Collider col)
{
if(col.tag == "Water")
{
// Exit water
}
}
I'm posting an answer rather than a comment since I can't comment yet.
Similar to what Andrew said, to prevent your game from thinking you're underwater as soon as your character puts its foot in the water, You could add an empty gameobject a few centimeters above your character's head and put a collider on that.
Then use Andrew's logic to determine that your character is underwater.
Related
The problem is that I what to create an openable door. This door should open when the player enter the Box Collider which is connected to the door. But the problem is when the door begins to open and to rotate, Collider starts to rotate too which brings me a lot of problems with usind such an idea. I try to create EmptyObject with its Collider but I can't connect this Collider with script and OnTriggerEnter function itself. Maybe I don't understand something, who knows, I'm just a begginer. How knows how to help, please write an answer.
My code if somebody needs it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class openDoor : MonoBehaviour {
public Vector3 Rotation_;
private int i;
public float speed;
bool opentheDoor;
bool closetheDoor;
// Use this for initialization
void Start () {
opentheDoor = false;
closetheDoor = false;
}
// Update is called once per frame
void Update () {
if (opentheDoor == true) {
this.transform.Rotate (Rotation_ * Time.deltaTime * speed);
i += 1;
if (i == 70) {
opentheDoor = false;
i = 0;
}
}
if (closetheDoor == true) {
this.transform.Rotate (-Rotation_ * Time.deltaTime * speed);
i += 1;
if (i == 70) {
closetheDoor = false;
i = 0;
}
}
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player") { {
opentheDoor = true;
}
}
}
void OnTriggerExit (Collider other) {
if (other.gameObject.tag == "Player") {
closetheDoor = true;
}
}
}
This is how i would handle the scenerio
Take
DoorHandler.cs
public class DoorHandler : MonoBehaviour {
public Door door;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
door.OpenDoor();
}
}
}
This should be attached to Parent of the door.
Next Take
Door.cs
public class Door : MonoBehaviour {
public bool isOpened = false;
public void OpenDoor()
{
if (!isOpened)
{
isOpened = true;
Debug.Log("OPEN");
//OPEN DOOR CODE!
}
}
}
Attach this to the Door GameObject
NOTE
The hierarchy would be like DoorHandler->Door->DoorModel (where Door is just an empty gameobject pivot of the Door)
In DoorHandler GameObject attach BoxCollider and Check Mark IsTrigger.
Also Player SHOULD HAVE A RIGIDBODY (preferably Kinametic) and obviously a collider
So When Player enters the DoorHandler's Collider -> The DoorHandler's OnTriggerEnter will be triggered and finally Call the Door to OpenDoor()
Add another check in OnTriggerEnter that checks if the door is currently opening or not.
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player" && !opentheDoor) {
opentheDoor = true;
}
}
attach the door to an empty object. put the trigger on the empty object. then make the ontrigger entry rotate the door, not the paent object, and the collider will remain in place.
Parent
-child(door)
-child(collider)
I'm trying to create objects that mark the cell that I can visit. I mark them with red square:
My code for creating objects:
using UnityEngine;
using System.Collections;
using System;
public class SpawnCheck : MonoBehaviour {
public GameObject checkObject;
public bool canSpawnCheck = true;
Vector2 boxSize;
public GameObject spawnedObject;
// Use this for initialization
void Start () {
Debug.Log("Into spawn check");
}
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Enter trigger collision");
canSpawnCheck = false;
if (other.gameObject.tag == "Target") {
Debug.Log ("Found Target");
}
if (other.gameObject.tag == "Wall") {
canSpawnCheck = false;
}
if (other.gameObject.tag == "Check") {
canSpawnCheck = false;
}
}
void OnTriggerExit2D(Collider2D other) {
Debug.Log("Exit trigger collision");
canSpawnCheck = true;
}
// Update is called once per frame
void Update () {
Debug.Log ("canSpawnCheck " + canSpawnCheck);
if (canSpawnCheck == true) {
Vector3 currentPosition = this.gameObject.transform.position;
Vector3 spawnPos = new Vector3 (Mathf.Round (currentPosition.x), Mathf.Round (currentPosition.y),0);
Debug.Log ("Physics.CheckSphere " + Physics.CheckSphere (spawnPos, 5));
if (!Physics.CheckSphere(spawnPos,5)) {
spawnedObject = (GameObject)Instantiate (checkObject, spawnPos, Quaternion.identity);
this.gameObject.GetComponentInParent<AILerp> ().possibleTargets.Add (spawnedObject);
}
}
}
}
My problem: as Physics.CheckSphere(spawnPos,5) always return false my code spawns too many red squares and spawn them upon each other. I want red squares to be created only once and never created on walls (white squares).
Your Check(Clone) GameObject has Box Collider 2D attached to it. Therefore every physics function you must use should be Physics2D.something not Physics.something. Notice the keyword there "2D".
If you use just Box Collider without the 2D in it, then you can use Physics.something. So, Physics.CheckSphere cannot be used with a 2D collider.
Check(Clone) is a SpriteRenderer, 2D Collider is appropriate. You just need to use one of the Physics2D overlap functions such Physics2D.OverlapBox, Physics2D.OverlapArea or Physics2D.OverlapCircle. Which ever one you prefer.
Title says it all.
I provided an NPC with the following script, which should make him look and bark at the player.
When the player comes into the NPC's reach, however, the NPC starts walking towards the player instead of just facing him.
Any thoughts?
using UnityEngine;
public class Barking : MonoBehaviour {
public AudioSource barkingAudio;
private GameObject player;
private bool barking;
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
barking = false;
}
void Update () {
if (barking)
lookAtPlayer();
}
private void lookAtPlayer()
{
transform.LookAt(player.transform.position, Vector3.up);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
barking = true;
barkingAudio.mute = false;
barkingAudio.Play();
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == player) {
barking = false;
barkingAudio.mute = true;
barkingAudio.Stop();
}
}
}
Since I was using a Rigidbody and rotating the transform manually, there was some unexpected behaviour.
I found some code online which I could replace the Transform.LookAt method with:
var qTo = Quaternion.LookRotation(player.transform.position - transform.position);
qTo = Quaternion.Slerp(transform.rotation, qTo, 10 * Time.deltaTime);
GetComponent<Rigidbody>().MoveRotation(qTo);
This fixed my problem!
I am trying to create a select object script in unity.
What it should do is when I hover over an object it colors red (and it does) and when I press "1" the GameObject's targetHighlighted will be filled with the object I am hovering over at that moment. In a Debug.Log this all works fine, targetHighlightedis filled.
When I press "1" however the targetHighlighted object is still left empty. It doesn't matter if I press it while hovering over the object or away from it.
My full code is much more extensive than this. But this section of code contains the problem, so I reduced it to this.
Can anyone explain to me how come when I press "1" the the Debug.Log doesn't show the targetHighlighted or targetSelected?
Basically why do the mouseenter and mouseexit log the right Object, but the setTarget function doesn't?
using UnityEngine;
using System.Collections;
public class TargetSelectionScript: MonoBehaviour {
// Store the current selected gameobject
GameObject targetHighlighted;
Renderer rend;
Color initialColor = Color.white;
Color selectedColor = Color.red;
public GameControllerScript gameController;
void Start() {
}
void Update() {
if (Input.GetKeyDown("1")) {
SetTarget();
}
}
void OnMouseEnter() {
SelectTarget();
}
void OnMouseExit() {
ClearTarget();
}
void SelectTarget() {
RaycastHit hitInfo = new RaycastHit();
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
targetHighlighted = hitInfo.transform.gameObject;
rend = targetHighlighted.GetComponent < Renderer > ();
rend.material.color = selectedColor;
Debug.Log("Highlighted target: " + targetHighlighted);
}
void ClearTarget() {
Debug.Log(targetHighlighted);
}
void SetTarget() {
Debug.Log(targetHighlighted);
}
}
Because no key is registered under "1"
for this reason, you must resort to using the appropriate KeyCode,
if (GetKeyDown(KeyCode.Alpha1))
SetTarget();
Try these changes:
GameObject targetHighlighted;
Renderer rend;
Color initialColor = Color.white;
Color selectedColor = Color.red;
public GameControllerScript gameController;
[SerializeField]
private bool targetSelected = false;
void Start() {
}
void Update() {
if (Input.GetKeyDown("1") && this.targetSelected == true) {
SetTarget();
}
}
void OnMouseEnter() {
SelectTarget();
}
void OnMouseExit() {
ClearTarget();
}
void SelectTarget() {
RaycastHit hitInfo;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
this.targetHighlighted = hitInfo.transform.gameObject;
rend = this.targetHighlighted.GetComponent < Renderer > ();
rend.material.color = selectedColor;
Debug.Log("Highlighted target: " + targetHighlighted);
this.targetSelected = true;
}
void ClearTarget() {
Debug.Log(this.targetHighlighted);
}
void SetTarget() {
Debug.Log(this.targetHighlighted);
}
I would like to make a gameobject in my game, which when enter trigger, then it will teleport my character to an another position (otherTeleport.transform.position), however when I use the script what I have written, my character won't teleport there, while the enemy will. If I put a Debug.Log in the bounce script which will log the character's position I see that my character has been at the otherTeleport's position, but only for one debuglog, and after that it's back to the original teleport's position. The teleport's would be working two-way so if the user go to one it will port it to the second, and if he goes to the second, then it will be ported to the first.
Here is my code:
public class TeleportScript : MonoBehaviour {
public GameObject otherTeleport;
private Collider otherTeleportColl;
public bool isNeedToTeleport;
private TeleportScript otherTeleportScript;
private GameObject player;
private Bounce bounceScript;
// Use this for initialization
void Start () {
isNeedToTeleport = true;
otherTeleportColl = otherTeleport.GetComponent<Collider>();
otherTeleportScript = otherTeleport.GetComponent<TeleportScript>();
player = GameObject.Find("Player");
bounceScript = player.GetComponent<Bounce>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider collider) {
if (isNeedToTeleport == true) {
if (collider.tag == "Enemy") {
otherTeleportScript.setNeedToTeleport(false);
collider.transform.position = otherTeleport.transform.position;
}
if (collider.tag == "Player") {
otherTeleportScript.setNeedToTeleport(false);
bounceScript.player.transform.position = otherTeleport.transform.position;
collider.transform.position = otherTeleport.transform.position;
bounceScript.playerTransform.position = otherTeleport.transform.position;
}
}
}
void OnTriggerExit(Collider collider) {
Debug.Log ("TELEPORTED OUT");
setNeedToTeleport(true);
otherTeleportScript.setNeedToTeleport(true);
}
public void setNeedToTeleport(bool value) {
if (value == true) {
isNeedToTeleport = true;
}
else {
isNeedToTeleport = false;
}
}
}
Any help would be appreciated!
I did some changes to your code, can't test it because I don't have access to unity right now, let me know if it works...
public class TeleportScript : MonoBehaviour {
public GameObject otherTeleport;
private Collider otherTeleportColl;
public bool isNeedToTeleport;
private TeleportScript otherTeleportScript;
private GameObject player;
private Bounce bounceScript;
// Use this for initialization
void Start () {
isNeedToTeleport = true;
otherTeleportColl = otherTeleport.GetComponent<Collider>();
otherTeleportScript = otherTeleport.GetComponent<TeleportScript>();
player = GameObject.Find("Player");
bounceScript = player.GetComponent<Bounce>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider collider) {
if(collider.tag == "Player" && isNeedToTeleport)
{
otherTeleportScript.setNeedToTeleport(false);
collider.transform.position = otherTeleport.transform.position;
bounceScript.player.transform.position = otherTeleport.transform.position; //not sure what is this for....
}
}
void OnTriggerExit(Collider collider) {
Debug.Log ("TELEPORTED OUT");
setNeedToTeleport(true);
otherTeleportScript.setNeedToTeleport(true);
}
public void setNeedToTeleport(bool value) {
if (value == true)
isNeedToTeleport = true;
else
isNeedToTeleport = false;
}
}
Your character teleports to the other place
after that you move the collider over there aswell, notice that the collider's object of other script of teleportation has been initialized with the isNeedToTeleport = true;
The character now also bounces there, hits the collider of the other object, it checks that isNeedToTelport is in fact true in this other script and it goes into the same If condition and teleports back!
Take this line:
otherTeleportScript.setNeedToTeleport(true);
out of your OnTriggerExit, or set it to false. Actually setting it to false might be better. You want to tell the other script that someone is incoming and it shouldn't immediately teleport them back.
As an aside, you should change your setNeedToTeleport function to this:
public void setNeedToTeleport(bool value) {
isNeedToTeleport = value;
}
Those ifs are redundant.