Ok tbh i don’t if it’s the right to put this or animation.
My problem is this. i am trying to make a lightswitch. i was able to make the script for the light to get activate with a raycast. Now i am trying to add an animation using the Animator. or should i make a script using vector? so here what i have.
if (open)
{
myLight.enabled = true;
{
if(GetComponent())
_anim.SetBool(“ON”, true);
}
}
else
{
myLight.enabled = false;
{
if (!GetComponent())
_anim.SetBool(“ON”, false);
Also, when i start game the switch just… loops on it’s own.
Let me know if this could work? If im in the wrong forum, or if a scrip without animator is better.
Code tags, please. And the whole code.
The tag of the light, the switch or the player?
Lightswitch Script
The Interactscript
Think it’s going to be best this way
I’m not gonna click external links. Put code tags in here and paste your code between them, in here.
nice try, next time like this…
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class LightSwitch : MonoBehaviour {
public Light myLight;
public bool open = false;
private Animator _anim;
void start()
{
_anim = GetComponent<Animator>();
}
public void ChangeLightState()
{
open = !open;
update();
}
void update()
{
if (open)
{
myLight.enabled = true;
{
if(GetComponent<Light>())
_anim.SetBool("ON", true);
}
}
else
{
myLight.enabled = false;
{
if (!GetComponent<Light>())
_anim.SetBool("ON", false);
}
}
}
}
using UnityEngine;
using System.Collections;
public class interactScript : MonoBehaviour {
public float interactDistance = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.F))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactDistance))
{
if (hit.collider.CompareTag("Switch"))
{
hit.collider.transform.parent.GetComponent<LightSwitch>().ChangeLightState();
}
}
}
}
}
And your light is no looping on its own? Without any interaction?
No the light is ok, i can press on and off np. But the animation plays back and forth. i want it to animate if the light gets open, and close if closed.
ok i fixed it. but if i press on the light switch nothing happens
You are getting button down, so it is calling it as long as you holding it down i think. Just try GetKey. Another reason could be your animation. Double check that they are not set to loop.
Forget about GetKey, GetKeyDown is okay too, just messed that up. anyway, check your animatino. what did you fix now?
The animation playing back in forth… animator is confusing.
your animation should not loop. and in the transitions in your animator thing, you need to be sure to set the bool right on both transitions, so on true, light on, on false, light off.
So i should make 2 boolean? thought i only needed one.
No, you only need one, but it seems like you didnt set up either your animator or animation right. As I understand, not one state is looping, it is turning on and off the whole time. So there is something wrong in setting your bools in your code or in the transitions, so just double check that.
ok there is no more animation loop. I’ll show you what i mean.
Would you look on external link like youtube?
Ya, youtube is okay. you can paste a link through the messages functions in here.