This code is attached to the sphere. and in the object hierarchy camera is below the sphere.
Below is my full Code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereMouseInput : MonoBehaviour {
// Use this for initialization
public GameObject gamePlane;
private Vector3 gameplaneRenderBound;
private double gameplaneWidth;
private double gameplaneHeight;
private Vector3 StartInput;
private Vector3 EndInput;
private Vector3 MouseVector;
private Vector3 MouseVectorNormal;
private Vector3 SphereDirection;
public Transform target;
private float speed = 5.0f;
private float dotVec;
void Start () {
StartInput = new Vector3(0.0f, 0f);
EndInput = new Vector3(0.0f, 0f);
gameplaneRenderBound = gamePlane.GetComponent().bounds.size;
gameplaneWidth = gameplaneRenderBound.x * 0.5; //x = 24
gameplaneHeight = gameplaneRenderBound.z + 5; // z = 40
//I just set to the some point. and made my spere to move forward.
// Camera is under my sphere.
SphereDirection = new Vector3(3.0f, 0.245f, 20.0f);
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
StartInput = Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0)){
EndInput = Input.mousePosition;
}
MoveThis();
}
void CalcutateMouseVector() {
MouseVector = EndInput - StartInput;
MouseVectorNormal = MouseVector;
MouseVectorNormal.Normalize();
dotVec= Vector3.Dot(MouseVectorNormal, Vector3.right);
Debug.Log(MouseVector.magnitude);
}
void MoveThis() {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, SphereDirection, step);
//if (transform.position == SphereDirection) step = 0;
//I tried to make sphere stop on the arrival but it didn’t work.
}
}