- Home /
Question by
JianZhang969 · Jan 10, 2021 at 06:44 PM ·
rotationraycastrotate objectswitchrotatearound
How Do I add rotations to one of my objects in my AR application?
Hello I recently started using unity and I wanted to create an AR application for my school project. For one of my objects, I wanted it to rotate on the 'z' axis when I tap on my phone's touch screen. I managed to get an image to pop up as well as an audio clip, but I can't find anything that can help me with my rotation problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Imagecon : MonoBehaviour
{
public AudioClip[] aClips;
public AudioSource myAudioSource;
string btnName;
public Image image;
int _rotationSpeed = 15;
// Use this for initialization
void Start()
{
myAudioSource = GetComponent<AudioSource>();
image.enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit Hit;
if (Physics.Raycast(ray, out Hit))
{
btnName = Hit.transform.name;
switch (btnName)
{
case "Standy_Button":
myAudioSource.clip = aClips[0];
myAudioSource.Play();
image.enabled = !image.enabled;
break;
case "Sphere":
myAudioSource.clip = aClips[1];
myAudioSource.Play();
break;
case "Arrow_Pointing_Right":
transform.Rotate(0, _rotationSpeed * Time.deltaTime, 0);
break;
default:
break;
}
}
}
}
}
Comment
Best Answer
Answer by JianZhang969 · Jan 14, 2021 at 10:34 AM
Managed to find a solution to my problem, I just attached the code that was under that specific switch case onto the object I wanted directly.
Code for future reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotation : MonoBehaviour
{
public float turnSpeed = 50f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Stationary)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit Hit;
if (Physics.Raycast(ray, out Hit))
transform.Rotate(Vector3.forward, turnSpeed * Time.deltaTime);
}
}
}
I attached this code onto the object directly and it worked.