- Home /
2D sprite and 3D model- swap visibility upon keystroke
Hey everyone, new user to Unity here. I'm working on a prototype for a new game where one of the main mechanics will be the player character swapping between a 2D sprite on the ground to a 3D model and vice versa upon hitting a key. Right now I have both objects moving in tandem and was wondering how I could set the visibility of the 3D model to start as false and swap between the sprite and model upon hitting a key like the spacebar.
Again, this is all fairly new to me, ESPECIALLY navigating C#, so spare no detail, I yearn to learn!
Like just disabling the renderer for the sprite while the model is active and then the opposite while you want the model active?
Precisely. If it turns out to be something super easy to do that would be grand. If it takes a lot of scripting, a detailed description of what each line does would really help me learn.
Answer by Crumpet · Feb 09, 2019 at 05:23 AM
that's very simple. Everything you can click/tick/adjust in the editor is simple to do with a script in unity. you'd just be looking for
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class renderer : MonoBehaviour
{
public SpriteRenderer sprenderer;
public MeshRenderer meshrenderer;
public bool tickme = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
//if blah blah blah happens
if (tickme)
{
sprenderer.enabled = false;
meshrenderer.enabled = true;
}
else
{
sprenderer.enabled = true;
meshrenderer.enabled = false;
}
}
}
Or if you are looking to disable the whole object, use GameObject and set them to active would be similar code just using public GameObjects and ins$$anonymous$$d using gameobject.setactive(true) or gameobject.setactive(false)
Answer by The-Z-axis · Feb 09, 2019 at 05:47 AM
Awesome!! Once I assigned the items I needed it worked great! One last question, how would I make it so that hitting the space bar check/unchecks "Tickme"?
if(Input.getkeyDown(keycode.Space)
{
Tickme = !Tickme;
}
didnt use a compiler and can't remember where capslock letters are so just rewrite it.
the unity script reference is pretty good so if you wanna know stuff its all on there for example if you want to know how to get mouse position you can google and it will come up with examples its good for simple stuff like this.
Your answer
Follow this Question
Related Questions
How i destroy the object of that part that collides with another object? 2 Answers
How to make keywords per material works properly (not in Editor) 0 Answers
スクリプトが追加できません。,スクリプトクラスが見つからない (I can't add a script. , Script class not found) 0 Answers
Failed to re-package resources Facebook-unity-sdk-7.11.1 2 Answers
Is it possible to have a changing line of script based off of variables? 0 Answers