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.
Related
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:
I'm trying to make a 2D sprite flip in a 3D scene, whenever the camera reaches an Y rotation of 180.
I can't get it to work, even though the TEST debug text shows whenever it reaches 180.
This is the code I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate_enemy : MonoBehaviour {
[SerializeField] SpriteRenderer spriteRenderer;
public bool flipX;
void Update()
{
float fYRot = Camera.main.transform.eulerAngles.y;
if (fYRot >= 180)
{
Debug.Log("TESTING");
spriteRenderer.flipX = true;
}
else
{
spriteRenderer.flipX = false;
}
}
}
I have a similiar script, that instead changes rotation based on velocity (the direction to which the character is going), and that script works just fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sprite_flip : MonoBehaviour {
[SerializeField] SpriteRenderer spriteRenderer;
public bool flipX;
Vector3 pos, velocity;
void Awake()
{
pos = transform.position;
}
// Update is called once per frame
void Update()
{
velocity = (transform.position - pos) / Time.deltaTime;
pos = transform.position;
if (velocity.x >= 0)
{
spriteRenderer.flipX = true;
}
else
{
spriteRenderer.flipX = false;
}
}
}
Anyone who might have an idea why the Rotate_Enemy script isn't doing what I want to achieve? Tried to find solutions but I couldn't make any of them work, so there must be something I don't understand. Super grateful for any help! :)
How do I create the Zoom-in and Zoom-out feature in Unity-3d using UI button click?
Solution depends on your specific setup, but this should be a good starting point, if you're using an ortographic Camera. If you're using a perspective camera instead, you should move its Transform's position forward or backwards. I'm also using Mathf.Clamp to restrict the zooming level to a specific range.
using UnityEngine;
using UnityEngine.UI;
public class Zoomer: MonoBehaviour
{
public Button zoomInButton;
public Button zoomOutButton;
public float zoomDelta = 0.1f;
public float minZoom;
public float maxZoom;
Camera cam;
void Start()
{
cam = Camera.main;
}
void OnEnable()
{
zoomInButton.onClick.AddListener(delegate { Zoom(-zoomDelta); });
zoomOutButton.onClick.AddListener(delegate { Zoom(zoomDelta); });
}
void Zoom(float value)
{
float v = Mathf.Clamp(
cam.orthographicSize + value,
minZoom,
maxZoom
);
cam.orthographicSize = v;
}
}
I have my player which uses character controller for moving. I placed a sprite in the scene and I'd like for when my player collides with the sprite to disable the sprite, like if the player grabs the sprite (which is Doom's 64 chainsaw).
The sprite's collisions of course work well with everything, but not with the player. How can I get proper collision between them?
You could do it like this:
1-Attach "Pickable" script to the sprite.
2-Attach "Player" script to the character controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickable : MonoBehaviour
{
public float radius = 1f;
private void Start()
{
SphereCollider collider = gameObject.AddComponent<SphereCollider>();
collider.center = Vector3.zero;
collider.radius = radius;
collider.isTrigger = true;
}
}
Here is the other script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Pickable pickable = other.GetComponent<Pickable>();
if(pickable != null)
{
Destroy(other.gameObject);
}
}
}
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.