- Home /
Emission Script
I'm having a bit of trouble I have two simple scripts.
One, is if I press the space key, the emission turns off and will turn on again if I let go of the space bar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlitsUit : MonoBehaviour
{
//
public Material material;
private void Update()
{
if (UnityEngine.Input.GetKey(KeyCode.Space))
{
material.DisableKeyword("_EMISSION"); ;
}
else
{
material.EnableKeyword("_EMISSION");
}
}
}
The other one is, If I press E, the emission will turn on and If I press D it will turn off.
They both work, but If i combine them, which is needed for the behaviour I want, they "D" Key doesn't work anymore. It will only turn off for a split second and come on again.
The code for the emission script is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KopLamp : MonoBehaviour
{
public Material material;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
material.EnableKeyword("_EMISSION");
}
if (Input.GetKeyDown(KeyCode.D))
{
material.DisableKeyword("_EMISSION");
}
}
}
Could someone help me with this? Thanks!
Answer by yummy81 · Apr 22, 2020 at 06:16 AM
The problem lies in the FlitsUit class. Look at the Update. If you hold down Spacebar, the following piece of code evaluates to true every frame:
if (UnityEngine.Input.GetKey(KeyCode.Space))
{
material.DisableKeyword("_EMISSION"); ;
}
But when you stop holding Spacebar down, the following piece of code evaluates to false and is executed every frame:
else
{
material.EnableKeyword("_EMISSION");
}
You can create some sort of communication between your scripts (I used bool "boo"), so they could better understand each other's behaviour:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KopLamp : MonoBehaviour
{
public Material material;
public bool boo;
void Update(){
if (Input.GetKeyDown(KeyCode.E)){
boo = false;
//material.EnableKeyword("_EMISSION");
}
if (Input.GetKeyDown(KeyCode.D)){
boo = true;
//material.DisableKeyword("_EMISSION");
}
}
}
and the second script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlitsUit : MonoBehaviour
{
public Material material;
KopLamp kopLamp;
void Awake(){
kopLamp = FindObjectOfType<KopLamp>();
}
void Update(){
if (Input.GetKey(KeyCode.Space)){
material.DisableKeyword("_EMISSION");
} else {
if (kopLamp.boo){
material.DisableKeyword("_EMISSION");
} else {
material.EnableKeyword("_EMISSION");
}
}
}
}
Thanks! That actually did the trick!
Since i'm still learning at this point, I have a other question aswell.
I want to play 2 animations but on the same key press. I made a script, but I think i'm overhtinking it.
The "b" key plays the first animations and if a user presses the key again it plays the seconds animation.
using UnityEngine;
using System.Collections;
public class Beugel : $$anonymous$$onoBehaviour
{
public Animator beugel;
// Use this for initialization
void Start()
{
beugel = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("b"))
{
if (Input.GetKey("b"))
{
beugel.Play("Beugel");
}
else
{
beugel.Play("BeugelOmlaag");
}
}
}
}
It plays the first animation just fine, but it wont play the second animation.
I'm not sure what i'm doing wrong here ..
Try this:
using UnityEngine;
using System.Collections;
public class Beugel : $$anonymous$$onoBehaviour
{
public Animator beugel;
bool boo;
void Start()
{
beugel = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.B))
{
boo ^= true;
if (boo){
beugel.Play("Beugel");
} else {
beugel.Play("BeugelOmlaag");
}
}
}
}
Thank you so much! This actually fixed my problem!