Rotating an object on button push

Updated on February 22, 2018 in [A] GUI
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on February 22, 2018

Hi guys, complete unity noob here.

https://imgur.com/a/kiHzc

As title suggests and as I’ve tried to illustrate in the images attached. I want a menu system ( I have this bit) that leads to a 3D object (lets say a machine of components) alongside a list of buttons.

What I’d like is to tap on a button and have the mesh rotate around to the relevant area and highlight the relevant component. Tap on the second component and have it move again to the relevant area an so on. Its too my understanding that I need to use transform.rotate (at least for the first bit) and I was advised that a Vector 3 script could be used for the rotation to be “smooth”. I am unsure as to how I can make each button rotate the mesh as needed.

Any help is greatly appreciated.

  • Liked by
Reply
1 on February 22, 2018

There are 2 ways I can think of. One way is easy to understand, but tedious to set up for larger objects, the other will automatically scale with any object, but it’s gonna require a LOT of vector math.

The easy way is to just assign a transform position to each of the components where you would want the camera to be when the object is selected, and then when they click the button, just lerp the camera position and rotation to it.

 

The other way takes a bit longer to type out, but if you’re interested in it, I can mock up something later.

 

Edit: If you don’t mind it not rotating the object, but instead just smooth snapping the camera to its proper position, there is a way that’s kind between the two in complexity, but just as scaleable as the second.

Wise
on February 22, 2018

I really quick wrote up that 3rd version I mentioned just to see what it would be like. It has a few issues, like it only really works if the component you want to look at parent is the center of the complete object, but I think It may work as a good starting point for what you’re trying to achieve.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFocus : MonoBehaviour
{
    private GameObject m_targetObject;
    public float m_cameraDistanceOffset;
    public float m_movementTime;
    private void Update()
    {
        RaySelect();
        FocusCameraOnObject(m_targetObject);
    }
    public void RaySelect()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayHit;
            if (Physics.Raycast(ray, out rayHit))
            {
                m_targetObject = rayHit.transform.gameObject;
            }
        }
    }
    public void FocusCameraOnObject(GameObject targetObject)
    {
        if(m_targetObject)
        {
            Vector3 newPos = m_targetObject.transform.parent.position + (m_targetObject.transform.localPosition.normalized * m_cameraDistanceOffset);
            Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, newPos, (Time.deltaTime / m_movementTime));
            Camera.main.transform.LookAt(Vector3.Lerp(Camera.main.transform.forward, newPos, (Time.deltaTime / m_movementTime)));
        }
    }
}

 

Just drop this on the object you want the camera to spin around, and let me know if this is something like what you’re going for.

Show more replies
  • Liked by
Reply
Cancel