Unity Right Click On Object - c#

I am new to unity. Currently, I am working on a small project.
I am creating a solar system where the player can click on planets and the main camera will zoom in and follow that planet.
The player can also right-click on a particular planet and the name of that planet will be displayed. However, if the player re-clicked the right click again, the name will disappear (need help with that)
I used OnMouseOver with Input.GetMouseButton (see below). But I am stuck and have no idea how to stop displaying the text when the player re-click right-click.
Can anyone please help me :)
What I have used:
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
text.SetActive(true);
}
}

You can use 1 variable to storage the current state and switch on/off state.
bool active = false;
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
active = !active;
text.SetActive(active);
}
}

Using an additional field like in this answer isn't even necessary.
You can simply do
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(1))
{
text.SetActive(!text.activeSelf);
}
}
See
GameObject.activeSelf

Related

How can I save the material of the player with playerprefs?

I have a game wehre you are a cube and you dodge obstacles, I just implemented the ability to change the color of the cube like changing skin. I did that by assigning a different material to the player when he presses "2".
Here is the script:
void Update()
{
if (Input.GetKeyDown("2"))
{
Object.GetComponent<MeshRenderer>().material = Material1;
}
}
When you die the scene resets and when you win a new scene is loaded, I would like the game to remember the material change even after the scene is reset or a new scene is loaded. I have done some research and found something called "PlayerPrefs" and I have been playing around with it but nothing even got close to working and I didn't really understand what i was doing.
I really want to understand how this works becuase I know i will be using it alot when making games. Can someone help me understand?
Thanks.
Create one gameobject and apply this script
public class SavingMaterial : MonoBehaviour
{
public static SavingMaterial instance;
public Material mat;
void Awake()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(base.gameObject);
}
else
{
Destroy(base.gameObject);
}
}
public void StoreMaterial(MeshRenderer mesh)
{
mat = mesh.material;
}
}
When you want to store material call this function like this
SavingMaterial.instance.StoreMaterial(you meshrenderer component);
and when you need this material just get from this class like this
material = SavingMaterial.instance.mat;
and note that if you quit the game you loose saved material as it is store in variable otherwise scene change and reset won't affect it.
you only save int,string and bool in playerprefs. you have to store values of materials into string and then save this string into playerprefs.
alternatively you can assign this material to local variable and set that script on Dontdestroyonload so it will not reset as scene is destroy or reset.

Unity3D Door Open/Close Bug

I have a problem in my project. I created an animation to open the door and made it open whenever a button was pressed. Then I copied the animation and entered its value -1 to make the door close after 2 seconds, but if the character presses the door open button again while playing the closing animation, it plays that animation and this causes a bug in the game. I apologize for telling a little complicated. I am waiting for your answers. Here is my code ->
public Text text;
public Animator anim;
private void Start()
{
text.enabled = false;
anim = GetComponent<Animator>();
}
private void Update()
{
if(text.enabled && Input.GetKeyDown(KeyCode.E))
{
anim.Play("DoorOpen");
}
}
private void OnTriggerExit(Collider other)
{
text.enabled = false;
}
private void OnTriggerEnter(Collider other)
{
text.enabled = true;
}
}
You shouldn't use the Animator Play() method and instead create parameters like a Trigger that.. well... triggers the animation.
Also, you don't say what's the error you're getting so it is hard to help you, but I guess the animation is restarting? You can set so the animations can't be transitioned to itself, so it won't restart if triggered again before finishing. Edit your AnimatorController asset, and click on the animation that shouldn't transition to itself, then uncheck where it says precisely that: "Can transition to itself". (Maybe you have to double click it, maybe you have to click on the transition, don't really remember right now, you'll figure it out :) ).

Unity2d Handle Click if GameObject not Covered

I want to allow clicks on a GameObject if that object is not covered.
I have tried:
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit2D = Physics2D.Raycast(Camera.main.transform.position,
Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (hit2D.collider)
{
executeLogic();
}
}
}
When covering the screen with an interstitial ad executeLogic() is still being run on click. How do I fix this?
I think you would need to maintain a flag inside AdManager script. When you Ad is shown you would require it to set to true and inside your logic for update you would have to use that boolean to stop executing logic.

A simple start button in Unity

Im making my first 2D game in unity and I have successfully made it. However I want a start button.I have looked up videos on how to make a start button and they all seem to have a start button on a different screen and once clicked it goes to another scene. Where as I would like my button to be on the same screen as my game and once clicked it starts the game, while having the button disappear. Can the code also be in c# thank you.
If you don't want to have different scenes (which, by the way, it's the recommended pattern) you can pause the game until the player presses the button. More informations can be found here.
using UnityEngine;
using UnityEngine.UI;
class GameManager: MonoBehaviour
{
public Button startButton;
private void Awake()
{
Time.timeScale = 0f;
}
private void OnEnable()
{
startButton.onClick.AddListener(StartGame);
}
private void OnDisable()
{
startButton.onClick.RemoveListener(StartGame);
}
private void StartGame()
{
Time.timeScale = 1f;
// Hides the button
startButton.gameObject.SetActive(false);
}
}
Put your button on your main scene that's it.

Utilizing the touchpad on gearVR in Unity

Still learning here and starting of with basics (just a plain Plane and moving as the first person around)
However i can run the app and look around and up and down etc. but can;t make the camera move when touching the touchpad on the GearVR headset.
I have created the script (c#) and attached to the camera in unity:
using UnityEngine;
using System.Collections;
public class Moving : MonoBehaviour {
// Use this for initialization
void Start()
{
if (Input.GetButton("Tap"))
{
// Do something if tap starts
}
if (Input.GetButtonUp("Tap"))
{
// Do something if tap ends
}
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Tap"))
{
// Do something if tap starts
}
if (Input.GetButtonUp("Tap"))
{
// Do something if tap ends
}
}
}
But it still doesn't seem to work. When i build and run the app it does nothing :-(
I know i am doing something wrong but not sure what.
Handling Single Tap in Oculus Gear VR:
void Start()
{
OVRTouchpad.Create();
OVRTouchpad.TouchHandler += OVRTouchpad_TouchHandler;
}
void OVRTouchpad_TouchHandler (object sender, System.EventArgs e)
{
OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
OVRTouchpad.TouchEvent touchEvent = touchArgs.TouchType;
if(touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
{
// Your response to Tap goes here.
}
}
Tap and Hold to Move:
You need to register Tapkey in your input following this
tutorial.
Add OVRPlayerController prefab to move around the scene. This will
let you move using keyboard W/A/S/D keys in Unity Editor.
Next you need to integrate Tap key with OVR Player controller script
to move in forward direction.
here are some useful links:
http://forum.unity3d.com/threads/gear-vr-touchpad-fps-script.373103/
http://rifty-business.blogspot.co.uk/2014/04/unity-pro-4-using-ovrcameracontroller.html

Categories

Resources