Hi.
I have just started to learn Unity and struggled with a raycast problem.
Currently I am producing a virtual space which allows the user to explore inside of a building (1st person perspective).
In this space, there would be some objects which interact with mouse.
For this, a collider is set on the floor of the building so that the camera would move according to the mouse hit point.
Also, I set a collider for each object and want to move the camera around this object when it is clicked.
However, when I click the object, the camera just moved toward the hit point on the floor passing the object.
The system seemed to recognise the hit point on the floor only ignoring the collider of the object.
Have tested this function changing “true” / “false” of “isMoveTowardObject” and “isMove” of the bellow script.
When “isMove = false” the camera moves and stops around the object.
However if “isMove = true”, the problem happened.
Assume that Raycast hit may function for one collider or there would be a certain order.
How can I move camera based on the hit point of the floor as well as the position of the ojbect??
Hope there would be some method to make it works…
Any advice and answer is welcomed
Thank you for your reading.
using UnityEngine;
using System.Collections;
public class ScMoveCamera : MonoBehaviour {
public float speed = 1.5f;
ScViewer view;
GameObject player;
ScManager manager;
RaycastHit Hit;
//Camera moves toward object
bool isMoveTowardObject = false;
//Camera moves toward hit point
bool isMove = false;
//isMove speed
float RotSpeed = 2.0f;
float moveSpeed = 6.0f;
//isMoveTowardObject speed
float RotSpeedobj = 3.0f;
float moveSpeedobj = 10.0f;
// Use this for initialization
void Start () {
//MoveCamera is an empty object which has a camera as a child
player = GameObject.Find ("MoveCamera");
view = player.GetComponent ("ScViewer") as ScViewer;
GameObject obj = GameObject.Find ("Manager");
manager = obj.GetComponent ("ScManager") as ScManager;
}
void Update () {
if (!view.isStart) {
//Manager is an empty object of which script allows picking of certain objects by mouse
GameObject obj = GameObject.Find ("Manager");
manager = obj.GetComponent ("ScManager") as ScManager;
if ( manager.isPicked && (Input.GetMouseButtonDown (0))) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (collider.Raycast (ray, out Hit, 100.0f)) {
Debug.DrawLine (ray.origin, Hit.point, Color.black);
// I want to make "MoveCamera" moved close to the object "1920s" when the user clicks "1920s"
if(Hit.transform.tag == "1920s"){
isMoveTowardObject = true;
}
// When the user clicks a certain point on the "Floor", "MoveCamera" moves to that hit point.
else if(Hit.transform.tag == "Floor"){
isMove =true;
}
}
}
}
if (isMove && !isMoveTowardObject) {
Vector3 targetPos = Hit.point;
targetPos.y = player.transform.position.y;
Quaternion targetDir = Quaternion.LookRotation(targetPos - player.transform.position);
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetDir, RotSpeed * Time.deltaTime);
if(Vector3.Distance(player.transform.position, targetPos) > 1.0f){
player.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
else{
isMove = false;
isMoveTowardObject = false;
}
}
else if (isMoveTowardObject) {
Vector3 targetPos = Hit.point;
targetPos.y = player.transform.position.y;
Quaternion targetDir = Quaternion.LookRotation(targetPos - player.transform.position);
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetDir, RotSpeedobj * Time.deltaTime);
if(Vector3.Distance(player.transform.position, targetPos) >10.0f){
player.transform.Translate(Vector3.forward * moveSpeedobj * Time.deltaTime);
}
else {
Debug.Log("stop movement");
isMoveTowardObject = false;
isMove = false;
//Application.LoadLevel ("1920s");
}
}
}
}