- Home /
how to change 2d image object when I tilt my smartphone
I want to do this... I want to change 2d image a to b when I tilt my smartphone.
I just start Unity in this month so I don't know how to solve this problem. Please help me. Thank you.
I tried this With two objects in hand. ①for rotated ②for trigger when this object collision
Problem It does not print "hit!!!" despite the collision.
I write code following this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColliderManager : MonoBehaviour
{
private Gyroscope m_gyro;
// Start is called before the first frame update
void Start()
{
Input.gyro.enabled = true;
}
void Update()
{
m_gyro = Input.gyro;
transform.rotation = m_gyro.attitude;
}
private void OnGUI()
{
var rect1 = new Rect(30, 30, 600, 50);
var rect2 = new Rect(30, 60, 600, 50);
GUI.skin.label.fontSize = 30;
GUI.Label(rect1, "attitude=" + m_gyro.attitude);
GUI.Label(rect2, "rotationRate=" + m_gyro.rotationRate);
}
private void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Cube2")
{
print("hit!!!");
}
}
}
I refer to following this sites.
Answer by melosoqa · Oct 07, 2020 at 12:41 AM
Thank you for read. I solved following code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColliderManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Input.gyro.enabled = true;
}
void Update()
{
Quaternion gyro = Input.gyro.attitude;
if (gyro.x > 0.5)
{
// X for you need
}
if (gyro.y > 0.5)
{
// Y for you need
}
}
}
Your answer
Follow this Question
Related Questions
Player will only shoot left, not right 0 Answers
Bullet speed changes depending on how close and far I click the mouse button 2 Answers
2d Top-Down Perlin noise map using Mathf.PerlinNoise 3 Answers
One 2D Raycast Being Weird While Others Work 0 Answers
Is PolygonCollider2D a legit way to check for mouse clicks in irregular shaped sprites? 1 Answer