how to make gameobject transparent using slider in unity - c#

I am trying to make gameobject transparent using slider, but transparency could not work after writing the script. my shader setting is legacy standard/diffuse. so what changes should I make to this script? and also what setting I should make along with the script code. please help me out to work with the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TransparentObject : MonoBehaviour
{
private GameObject objectTotransparent;
public float alpha = 0.3f;
//private float increaseAlpha;
//private float decreaseAlpha;
public Slider transparentSlider;
//renderer attached to the object that you want to make transparent
//public Renderer rend;
private Material currentMat;
/*void Awake()
{
transparentSlider.onValueChanged.AddListener(OnSliderChanged);
}*/
void Start()
{
//objectTotransparent = gameObject;
//currentMat = objectTotransparent.GetComponent<Renderer>().material;
AsistantControllScript = FindObjectOfType<AsistantControll>();
currentMat = currentGameObject.GetComponent<Renderer>().material;
}
void Update()
{
//ChangeAlpha(currentMat, alpha);
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.current.ScreenPointToRay(touch.position);
RaycastHit hitObject;
if (Physics.Raycast(ray, out hitObject))
{
objectTotransparent =
hitObject.transform.parent.transform.parent.gameObject;
objectTotransparent.GetComponent<Recolour>().SetSelected();
}
}
}
void ChangeAlpha(Material mat, float alphaVal)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
mat.SetColor("_Color", newColor);
}
public void ChangeAlphaOnValue(Slider slider)
{
ChangeAlpha(currentMat, slider.value);
}
public void Deselect()
{
objectTotransparent.GetComponent<Recolour>().SetOriginalMaterial();
objectTotransparent = null;
transparentSlider.value = alpha;
}
}

Change your material Rendering Mode into Transparent:

Related

I want to make my player move from point A to point B, execute animation and come back

I want to make my player move from point A to point B, execute animation and come back I am making a turned based battle system and I want my player character to move to the enemy character execute the attack and come back to its original transform. I want this done in a click of a button and I just can't seem to figure it out. I tried vector2.lerp and MoveTowards but both had to be in update to work or maybe I just missed something because I am relatively new to Unity
Source code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using CodeMonkey.HealthSystemCM;
public class BattleHandler : MonoBehaviour
{
public GameObject playerCharacter;
public GameObject enemyCharacter;
private GameObject playerPrefab;
private GameObject enemyPrefab;
public CharacterBase playerCharacterBase;
public CharacterBase enemyCharacterBase;
private HealthSystemComponent enemyHealth;
private HealthSystemComponent playerHealth;
private ParticleSystem addStrenghtParticle;
private Transform playerPosition;
private Transform enemyPosition;
private void Start()
{
SpawnCharacter(true);
SpawnCharacter(false);
playerCharacterBase = playerPrefab.GetComponent<CharacterBase>();
enemyCharacterBase = enemyPrefab.GetComponent<CharacterBase>();
enemyHealth = enemyPrefab.GetComponent<HealthSystemComponent>();
playerHealth = playerPrefab.GetComponent<HealthSystemComponent>();
addStrenghtParticle = playerPrefab.GetComponentInChildren<ParticleSystem>();
playerPosition = playerPrefab.GetComponent<Transform>();
enemyPosition = enemyPrefab.GetComponent<Transform>();
}
public void AttackMovement()
{
playerPosition.position = Vector2.Lerp(playerPosition.position, enemyPosition.position, Time.deltaTime);
}
public void AddStrenght()
{
playerCharacterBase.AddStrenght();
addStrenghtParticle.Play();
}
public void AttackSequence()
{
Invoke("PlayerAttack", 0f);
Invoke("EnemyAttack", 1f);
}
private void PlayerAttack()
{
float damage1 = playerCharacterBase.CalcDamage();
playerCharacterBase.AttackAnim();
enemyCharacterBase.HurtAnim();
enemyHealth.Damage(damage1);
enemyHealth.Die();
}
private void EnemyAttack()
{
float damage2 = enemyCharacterBase.CalcDamage();
enemyCharacterBase.AttackAnim();
playerCharacterBase.HurtAnim();
playerHealth.Damage(damage2);
playerHealth.Die();
}
public void SpawnCharacter(bool isPlayerTeam)
{
Vector3 position;
if (isPlayerTeam)
{
position = new Vector3(-6, 15);
playerPrefab = Instantiate(playerCharacter, position, Quaternion.identity);
}
else
{
position = new Vector3(4, 15);
enemyPrefab = Instantiate(enemyCharacter, position, Quaternion.identity);
}
}
}

Make 3d gameobject object transparent using slider in unity

I am trying to make multiple object transparent using a slider but not all at the same time. What is it ,mean is that the selected object should be able to have a transparent shade on it. I have written a code for it. But I need help to identify my mistake or correct me in the code.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TransparentObject : MonoBehaviour
{
private GameObject objectTotransparent;
public float alpha = 0.5f;
private Material currentMat;
public Slider transparentSlider;
void Awake()
{
transparentSlider.onValueChanged.AddListener(OnSliderChanged);
}
//void Start()
//{
//objectTotransparent = gameObject;
//currentMat = objectTotransparent.GetComponent<Renderer>().material;
//}
void Update()
{
//ChangeAlpha(currentMat, alpha);
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.current.ScreenPointToRay(touch.position);
RaycastHit hitObject;
if (Physics.Raycast(ray, out hitObject))
{
objectTotransparent = hitObject.transform.parent.transform.parent.gameObject;
objectTotransparent.GetComponent<Recolour>().SetSelected();
}
}
}
/*void ChangeAlpha(Material mat, float alphaVal)
{
}*/
void OnSliderChanged(float alphaVal)
{
//ChangeAlpha(currentMat, transparentSlider.value);
//Color oldColor = mat.color;
//Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
//mat.SetColor("_Color", newColor);
transparentSlider = GUI.HorizontalSlider( Rect(20,135,175,30), transparentSlider, 1, 0);
renderer.material.color.a = transparentSlider;
}
public void Deselect()
{
objectTotransparent.GetComponent<Recolour>().SetOriginalMaterial();
objectTotransparent = null;
transparentSlider.value = alpha;
}
}
In the OnSliderChanged method, try setting the alpha component to transparentSlider.value instead of the Slider object. Here's how you should change the transparency of an object in a simple script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeTransparency : MonoBehaviour
{
//renderer attached to the object that you want to make transparent
public Renderer rend;
//ui slider that you want to use to controll the transparency
public Slider slider;
private void Update()
{
rend.material.color = new Color(
rend.material.color.r,
rend.material.color.g,
rend.material.color.b,
slider.value
);
}
}
NOTE: The material that is applied to the object that you want to make transparent should have its Rendering Mode set to Transparent:
Inspector for the material:
Now you can change the albedo and all the other parameters to mimic your original material.

The cube does not rotate by pressing Vuforia virtual button in Unity3d

I added the cube on the image target of Vuforia. I also added the virtual button on the image target. Now I want to rotate the cube by pressing virtual button. For this purpose I implemented the following script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class rotate : MonoBehaviour, IVirtualButtonEventHandler {
public GameObject vbtn;
public GameObject cube;
public Renderer rend;
// Use this for initialization
void Start () {
vbtn = GameObject.Find ("virtualbtn5");
vbtn.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
cube = GameObject.Find ("Cube");
rend = cube.GetComponent<Renderer>();
}
public void OnButtonPressed(VirtualButtonBehaviour vb){
Debug.Log ("Button pressed");
cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
rend.material.color = Color.blue;
}
public void OnButtonReleased(VirtualButtonBehaviour vb){
Debug.Log ("Button released");
rend.material.color = Color.red;
}
}
The button seems to be working because the Debug.Log ("Button pressed"); and rend.material.color = Color.blue; statements in onButtonPressed function are working fine. But cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0)); for rotating cube does not work.
Simple is that the button can change the color of the cube but it does not rotate the cube.
So the question is How would I rotate the cube by pressing virtual button of vuforia.
Question Updated:
I also tried the following code but still the cube does not rotate on button press.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class rotate : MonoBehaviour, IVirtualButtonEventHandler {
public GameObject vbtn;
public GameObject cube;
public Renderer rend;
public bool rotateit;
public float speed;
// Use this for initialization
void Start () {
vbtn = GameObject.Find ("virtualbtn5");
vbtn.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
cube = GameObject.Find ("Cube");
speed = 100f;
rend = cube.GetComponent<Renderer>();
rotateit = false;
}
void Update(){
if (rotateit) {
cube.transform.Rotate(new Vector3(0, Time.deltaTime * speed, 0));
}
}
public void OnButtonPressed(VirtualButtonBehaviour vb){
//Debug.Log ("Button pressed");
//cube.transform.RotateAround(cube.transform.position, new Vector3(0, 1, 0), 10000f * Time.deltaTime);
//cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
rend.material.color = Color.blue;
rotateit = true;
Debug.Log ("Button pressed "+rotateit);
}
public void OnButtonReleased(VirtualButtonBehaviour vb){
//Debug.Log ("Button released");
rend.material.color = Color.red;
rotateit = false;
Debug.Log ("Button released "+rotateit);
}
}
Also have a look at console window
If you want to rotate every frame while the button is held down but stop when released then use a boolean variable to do that. Set it to true in OnButtonPressed and false in OnButtonReleased. Check if this flag in true in the Update function then rotate the cube.
public GameObject vbtn;
public GameObject cube;
public Renderer rend;
bool pressed = false;
public float speed = 100f;
// Use this for initialization
void Start()
{
vbtn = GameObject.Find("virtualbtn5");
vbtn.GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
cube = GameObject.Find("Cube");
rend = cube.GetComponent<Renderer>();
}
public void OnButtonPressed(VirtualButtonBehaviour vb)
{
Debug.Log("Button pressed");
pressed = true;
rend.material.color = Color.blue;
}
public void OnButtonReleased(VirtualButtonBehaviour vb)
{
Debug.Log("Button released");
pressed = false;
rend.material.color = Color.red;
}
void Update()
{
if (pressed)
cube.transform.Rotate(new Vector3(0, Time.deltaTime * speed, 0));
}

Get PointerDown on 2D UI Element in Unity C#

I've been through number of answers on this topic, but nothing has seemed to work for my case. I'm trying to detect a mouseDown on a UI element with a canvas renderer which is inside the hierarchy of an object with the canvas on it. I'm new to this, so I'm not sure if the canvas needs to be linked to this canvas renderer or if they have to be on the same object, but the following code is not resulting in the OnPointerDown method being activated.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class Knob : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public Slider slider;
public GameObject pivot;
public bool selected;
public float z;
public Image image;
public UtilityClass the;
public float min;
public float max;
void Start () {
z = 90;
Quaternion rotation = Quaternion.Euler (0, 0, z);
pivot.transform.rotation = rotation;
}
void Update() {
Vector3 mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
if (selected) {
float pivotAngle = the.AngleFrom (the.Heading (pivot.transform.position, mousePosition));
if (pivotAngle >= min && pivotAngle <= max)
z = pivotAngle;
Quaternion rotation = Quaternion.Euler (0, 0, z);
pivot.transform.rotation = rotation;
}
}
public void OnPointerDown(PointerEventData eventData) {
selected = true;
}
public void OnPointerUp(PointerEventData eventData) {
selected = false;
}
}
At the moment I don't have a Collider 2D on the object, but I have "Raycast Target" selected in the Image script. Any help would be appreciated.
Credit to Juan Bayona Beriso I was missing an Event System Component.

Changing the opacity of a transparent texture

I have a transparent texture of a chain link fence. I want the fence to fade in as the player approaches from the z direction. The problem I am having is that because the fence is transparent the opacity slider disappears and uses the image transparency. (I want the transparent texture to fade in) My current code:
public class WallFader : MonoBehaviour {
public GameObject wallone;
private Vector3 wallonetransform;
private Color wallonecolor;
public GameObject player;
private Vector3 playerposition;
private float PPX;
private float PPZ;
// Use this for initialization
void Start()
{
wallonetransform = wallone.GetComponent<Transform>().position;
wallonecolor = wallone.GetComponent<Renderer>().material.color;
}
// Update is called once per frame
void Update () {
playerposition = player.transform.position;
PPX = playerposition.x;
PPZ = playerposition.z;
// Distance to the large flat wall
float wallonedist = wallonetransform.z - PPZ;
if (wallonedist > 10)
{
wallonecolor.a = 0;
}
else
{
//fade in script
}
}
The fence never fades or disappears when wallonedist is > 10
Color is a struct which means that changing it won't change the instance of of the Renderer. It is a copy of a color from the Renderer. If you change the color, you have to re-assign the whole color back to the Renderer for it to take effect.
public class WallFader : MonoBehaviour
{
public GameObject wallone;
private Vector3 wallonetransform;
private Color wallonecolor;
Renderer renderer;
public GameObject player;
private Vector3 playerposition;
private float PPX;
private float PPZ;
// Use this for initialization
void Start()
{
wallonetransform = wallone.GetComponent<Transform>().position;
renderer = wallone.GetComponent<Renderer>();
wallonecolor = wallone.GetComponent<Renderer>().material.color;
}
// Update is called once per frame
void Update()
{
playerposition = player.transform.position;
PPX = playerposition.x;
PPZ = playerposition.z;
// Distance to the large flat wall
float wallonedist = wallonetransform.z - PPZ;
if (wallonedist > 10)
{
wallonecolor.a = 0;
renderer.material.color = wallonecolor; //Apply the color
}
else
{
//fade in script
}
}
}

Categories