Detect tap in Samsung Gear VR - c#

how can I detect tap in Gear Vr to make an action
I use unity 5 with C# programming language
my tries
I read answers in untiy3d forums
none of them work to me
http://forum.unity3d.com/threads/samsung-gear-vr-detect-tap-swipe.298346/
any suggestions

You have to implement the tap, (or in reality a click, as the touchpad works as a mouse) yourself. A tap is a touch/mouse down, and then a touch/mouse up in a relatively same place.
Here's some untested code that should work (call if it doesn't):
using UnityEngine;
public class ClickDetector:MonoBehaviour {
public int button=0;
public float clickSize=50; // this might be too small
void ClickHappened() {
Debug.Log("CLICK!");
}
Vector3 pos;
void Update() {
if(Input.GetMouseButtonDown(button))
pos=Input.mousePosition;
if(Input.GetMouseButtonUp(button)) {
var delta=Input.mousePosition-pos;
if(delta.sqrMagnitude < clickSize*clickSize)
ClickHappened();
}
}
}

Thanks to #chanibal I find answer
Input.GetMouseButtonDown(0)
but I face another problem , application crush
is there any custom configuration to Gear VR

Related

Unity 3d GO orientation

I look for the best way to detect, what site of an GO (cube) is facing upwards.
My research led me to the Dot-product.
I know what I want to do, but I guess my c# skills are too bad..
Basically I just want to find the Dot-product for example for the x-rotation.
And if its between a value of 0,9 to 1 one site is facing upwards, for -0,9 to -1 the other Site and so on for all axes.
In the Unity's documentation you have the following example. You just have to tweak it a little bit in order to achieve what you need.
// detects if other transform is behind this object
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Update()
{
if (other)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = other.position - transform.position;
if (Vector3.Dot(forward, toOther) < 0)
{
print("The other transform is behind me!");
}
}
}
}

Controlling Light components C#

I have a game mechanic where the player can toggle the car headlights with the L keypress and the backlights witht he S key, which also controls backwards movement.
This is shown in the code below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class headLights : MonoBehaviour {
private Light frontLight;
private Light backLight;
// Use this for initialization
void Start () {
frontLight = GetComponent<Light>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
frontLight.enabled = !frontLight.enabled;
}
if (Input.GetKeyDown(KeyCode.S))
{
backLight = GetComponent<Light>();
backLight.enabled = !backLight.enabled;
}
}
}
The problem is when I press L or S, both the front and back lights turn on because which I assume, the GetComponent refers to all extra Light components in the Scene and generalizes them as one.
I want to get the S key to only turn on the "backLights" while it is pressed and the L key to only toggle the "frontLights".
METHODS I HAVE USED TO TRY FIX THE PROBLEM
frontLight = GameObject.Find("Player").GetComponent<Light>();
This code just gives me errors like "the gameobject player does not have any light components attached to it(although it clearly does) blah blah blah.
I have also tried using tags but they confuse me a lot and seem like the easy way out. I know in the future I will have to learn how to do object orientated syntax and coding so I would very much like to learn how to reference it!
Please help me if you can, it would make my day~~ :)
Please note, you do not have to solve the whole problem for me if you are short on time, just giving general syntax that I can just swap out would help me a great deal!
Its really easy to disambiguate the two lights. Just make the variables public and set them in the inspector. Be sure to null check them before you use them to make sure they are set.
I just realized that probably didn't make sense.
Lets say your hierarchy is set up like this with the light objects as children to the car:
car
+-FrontLight
+-RearLight
Instead of putting the custom behavior on the light gameobjects, you should put it on the car.
Then, the behavior would look like this:
public class headLights : MonoBehaviour {
public Light frontLight;
public Light backLight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
if(frontLight != null) {
frontLight.enabled = !frontLight.enabled;
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if(backLight != null) {
backLight.enabled = !backLight.enabled;
}
}
}
This is because the lights are not Components of the car, they are Children of it.

Send player position to another player to move him in multi player game using Unity Photon

I follow the steps of PUN basic tutorial.
For now I reach to a part of what I want Send my position in the current time to another player with me in the room to move him. I can print my position each time I update it , what I need is to know how I can send position to another player to move him.
Let's say I have a desktop player and when I move him this translation moving the player on mobile.
And how I stop instantiate object on mobile, I just want to deal with the instantiated object on desktop.
I am using unity and Photon Network SDK.
Here is the code I used
using UnityEngine;
using System.Collections;
public class NetworkCharacter : Photon.PunBehaviour {
private Vector3 correctPlayerPos;
void Update()
{
if (!photonView.isMine)
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
photonView.RPC ("PrintPosition", PhotonTargets.All, transform.position);
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
// We own this player: send the others our data
stream.SendNext(transform.position);
}
else
{
// Network player, receive data
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
}
}
[PunRPC]
void PrintPosition(Vector3 pos)
{
Debug.Log (pos);
//I need to send position coordinates to the other device
}
}
The other class of establish multiplayer environment:
using UnityEngine;
using System.Collections;
public class NetworkManager : Photon.PunBehaviour {
// Use this for initialization
void Start () {
PhotonNetwork.ConnectUsingSettings ("0.1");
//PhotonNetwork.logLevel = PhotonLogLevel.Full;
}
void Update () {
}
void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
public override void OnJoinedLobby ()
{
Debug.Log("join lobby!");
PhotonNetwork.JoinRandomRoom ();
}
void OnPhotonRandomJoinFailed()
{
Debug.Log("Can't join random room!");
PhotonNetwork.CreateRoom(null);
}
void OnJoinedRoom()
{
Debug.Log("join random room!");
GameObject monster = PhotonNetwork.Instantiate("monsterprefab", Vector3.zero, Quaternion.identity, 0);
}
}
PUN has some new components since their new update.
I'd recommend you to use those components because it is really user friendly towards new users.
Some components you could use:
PhotonTransformView
PhotonRigidbodyView
PhotonAnimatorView
These three components will help you sync your position, physics and animations on the GameObject it is attached to.
If you do not want to use these components i suggest you search up: interpolation and extrapolation for Unity PUN.
Some good starting tutorials:
SkyArena PUN Tutorial This is a really good video tutorial[in parts] on PUN so you should definitely check that out.
Exit Games Marco Polo This tutorial is created by someone from PUN also really good to read even if you don't need it.
Hope this will help you.
-Menno
I found the answer in Demo Synchronization that provided in PUN plugin, it is really helpful.

unity - mobile touch control to drag gameobject in 2D game

I wanted to find out what is the best-practice method to use for touch controls for mobile 2D games, specifically dragging a paddle for breakout/arkanoid style games. Would be surprised if there are no easy built in Unity facility to do this as I would have thought this would have been a standard feature given the popularity of mobile games.
Any advice or links to tutorials, especially dragging game objects, would be greatly appreciated. Thanks.
My advice would be to use the unity UI features (introduced in Unity 4.6) using a script like this one :
public class DragControl : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
private bool m_Used;
private Vector3 m_MouseStartPosition;
private Vector3 m_ItemStartPosition;
public void Start()
{
m_Used = false;
}
public void Update()
{
if(m_Used)
{
GetComponent<RectTransform>().anchoredPosition = new Vector2(m_ItemStartPosition.x + (Input.mousePosition.x - m_MouseStartPosition.x), m_ItemStartPosition.y + (Input.mousePosition.y - m_MouseStartPosition.y));
// or you can set the y value to m_ItemStartPosition.y only if you don't want the paddle to move on Y axis
}
}
public void OnPointerDown(PointerEventData eventData)
{
m_Used = true;
m_MouseStartPosition = Input.mousePosition;
m_ItemStartPosition = GetComponent<RectTransform>().anchoredPosition;
}
public void OnPointerUp(PointerEventData eventData)
{
m_Used = false;
}
}
For this to work you have to enable the UI item raycasting (keep "Raycast Target" checked) and nothing blocking the raycast being over your item in the UI layers.
Also note that if you don't need it you can disable mutli touch input using Input.multiTouchEnabled = false;.
An easy solution would be to import the now standard (Unity 5?) "DragRigidbody" script and attach it to your Game Object. Just make sure your object has a RigidBody Component attached to it (which I would assume your paddle object would have already).

Unity - Resizing one GameObject to match another

I'm working on a project in Unity that involves regions that teleport any non-static object from one to the paired. That part's fine, but for convenience, I'm trying to write a part of the script that will resize one object if its pair is resized, such that they will always be of equal size. And so far it works - mostly. The only problem I encounter is when trying to resize through the Transform component - as in, typing in numbers in Inspector, or using the value sliders on X or Y or Z. The handles work fine. It's not a big deal, I suppose, but if I could figure out why this isn't working, so I can learn what to do in the future, I'd be very glad. Here's the code:
[ExecuteInEditMode]
public class TransferRegion : MonoBehaviour {
// Unrelated code...
public bool scaleManuallyAltered {
get; private set;
}
[SerializeField]
private TransferRegion pair;
private Vector3 scale;
// Called whenever the scene is edited
void Update () {
if (scale != gameObject.transform.localScale) {
scaleManuallyAltered = true;
scale = gameObject.transform.localScale;
}
if (pair && scaleManuallyAltered && !pair.scaleManuallyAltered) {
pair.transform.localScale = scale;
}
}
// Called AFTER every Update call
void LateUpdate () {
scaleManuallyAltered = false;
}
// Unrelated code...
}
If anyone can see some major logical failure I'm making, I'd like to know. If my code's a bit hard to understand I can explain my logic flow a bit, too, I know I'm prone to making some confusing constructs.
Thanks folks.
If you want one object to be the same scale as another, why not just simplify your code by setting the scale of the re sizing game object, directly to the scale of the game object it is based off of? For example, this script re sizes an object to match the scale of its pair while in edit mode:
using UnityEngine;
using UnityEditor;
using System.Collections;
[ExecuteInEditMode]
public class tester : MonoBehaviour
{
public Transform PairedTransform;
void Update()
{
if (!Selection.Contains(gameObject))
{
gameObject.transform.localScale = PairedTransform.localScale;
}
}
}
I tested this on two cubes in my scene. I was able to resizing using gizmos as well as manually typing in numbers to the transform edit fields in the inspector.
Edit: By taking advantage of Selection you can apply the scale change only to the object in the pair that is not selected in the hierarchy. This way the pairs wont be competing with each other to re scale themselves.
So I figured it out.
I'm not sure what was wrong with my original code, but eventually I decided to slip into the realm of good old handy events:
[ExecuteInEditMode]
public class TransferRegion : MonoBehaviour {
//...
[SerializeField]
private UnityEvent rescaled;
//...
void Update() {
if (scale != gameObject.transform.localScale) {
scale = gameObject.transform.localScale;
rescaled.Invoke();
}
}
//...
public void OnPairRescaled() {
gameObject.transform.localScale = pair.transform.localScale;
scale = gameObject.transform.localScale;
}
}
And just set the OnPairRescaled event to be the listener for the paired object's rescaled event.
Ta-da!

Categories