using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moving : MonoBehaviour {
public float speed = 4f;
public float radius = 5f;
Vector3 positionOnScreen;
Vector3 mouseOnScreen;
void Update () {
positionOnScreen = Camera.main.WorldToViewportPoint (transform.position);
mouseOnScreen = (Vector3)Camera.main.ScreenToViewportPoint(Input.mousePosition);
print (positionOnScreen);
//Get the angle between the points
float angle = -90-Mathf.Atan2(positionOnScreen.y-mouseOnScreen.y, positionOnScreen.x - mouseOnScreen.x) * Mathf.Rad2Deg;
//Rotate object to the angle
transform.rotation = Quaternion.Euler (new Vector3(0f,angle,0f));
Vector3 input = new Vector3 (Input.GetAxisRaw("Horizontal"), 0 ,0).normalized;
transform.Translate (input * Time.deltaTime * speed);
}
void OnDrawGizmos() {
Gizmos.DrawSphere (positionOnScreen, 2f);
}
}
This code was made to Rotate obj in local space by mouse input. While in editor positionOnScreen drawn at 0,0,0; As soon as I hit play transform of positionOnScreen changes to 0.5, 0.2, 23.6. I undestand that it depends on main camera position but cant get the exact dependece between them. I read manual btw. Don’t get the concept of it still.