Dims
Dims

Reputation: 51229

How to make player to be able to rotate camera around cube in Unity?

I have created new Unity project, have added a cube into the center and now want to make player to be able to rotate camera around this cube by swipes and/or mouse drags.

Please, name simple steps to implement this or keywords to find an answer or where to read about it?

Upvotes: 3

Views: 2702

Answers (3)

Dims
Dims

Reputation: 51229

I wrote this way:

float mouseX = -Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");

float magnitude = transform.position.magnitude;

Vector3 mouseSwipe = new Vector3(mouseX, mouseY, 0);
Vector3 startPoint = new Vector3((float)camera.pixelWidth / 2, (float)camera.pixelHeight / 2, magnitude - 1);

Vector3 startPointWorld = camera.ScreenToViewportPoint(startPoint);
Vector3 endPointWord = camera.ScreenToViewportPoint(startPoint + mouseSwipe);

Vector3 mouseSwipeWord = endPointWord - startPointWorld;

float dragLat = mouseSwipeWord.y;
float dragLng = mouseSwipeWord.x;

Vector3 oldPosition = transform.position / magnitude;

float lat = Mathf.Asin(oldPosition.y);
float rsmall = Mathf.Acos(oldPosition.y);

float lng = Mathf.Atan2(oldPosition.z / rsmall, oldPosition.x / rsmall);

lat += dragLat * 10 * 2 * Mathf.PI;
if( lat*180/Mathf.PI > 80 )
{
    lat = 80 * Mathf.PI / 180;
}
else if( lat*180/Mathf.PI < -80)
{
    lat = -80 * Mathf.PI / 180;
}

lng += dragLng * 10 * 2 * Mathf.PI * 2;



float y = Mathf.Sin(lat);
rsmall = Mathf.Cos(lat);

float x = rsmall * Mathf.Cos(lng);
float z = rsmall * Mathf.Sin(lng);

Vector3 newPosition = new Vector3(x, y, z);
newPosition *= magnitude;

transform.position = newPosition;

LookAtTarget();

The goal was to simulate mouse is rotation object by drag.

Upvotes: 0

Vinicius Sena
Vinicius Sena

Reputation: 71

Create a new C# Script called "CameraRotate", open it and paste this script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotate : MonoBehaviour {

    public Transform cube;

    public float sensitivityX;
    public float sensitivityY;
    GameObject pivot;

    void Start () {
        pivot = new GameObject ("pivot");
        pivot.transform.position = cube.position;
        transform.SetParent (pivot.transform);
    }

    void Update () {
        pivot.transform.eulerAngles += new Vector3 (Input.GetAxis ("Mouse Y") * -sensitivityX * Time.deltaTime, Input.GetAxis ("Mouse X") * sensitivityY * Time.deltaTime, 0);
    }
}

Assign this script to your camera, then in Inspector Tab, with the camera selected, assign your Cube (drag) into the "cube" slot, and assign a value to the sensitivity fields (i used something about 500), press play and test.

What does this script do? It creates a new GameObject at the center of the Cube to be a reference of rotation, it's called pivot. So it sets the Camera as a child of the pivot, and rotates the pivot according with your mouse axis.

Upvotes: 0

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

public Transform Target;

public float distance = 2.0f;
public float xSpeed = 20.0f;
public float ySpeed = 20.0f;
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
public float distanceMin = 10f;
public float distanceMax = 10f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;

void Update()
{
    if (Input.GetMouseButton(0))
    {
        velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
        velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
    }
    rotationYAxis += velocityX;
    rotationXAxis -= velocityY;
    rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);

    Quaternion rotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);

    Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    Vector3 position = rotation * negDistance + Target.position;

    transform.rotation = rotation;
    transform.position = position;
    velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
    velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}

public static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360F)
        angle += 360F;
    if (angle > 360F)
        angle -= 360F;
    return Mathf.Clamp(angle, min, max);
}

Taken from https://answers.unity.com/questions/1257281/how-to-rotate-camera-orbit-around-a-game-object-on.html

If you only want to rotate around a specific axis, for example around Y, you could basicly just do this

this.transform.RotateAround(Target.transform.position, Vector3.up, Input.GetAxis("Mouse X")*20.0f);

Upvotes: 3

Related Questions