I want to make the camera go downwards when its ray gets hit to any object on top.
So the following code is the code I have written for it. It works but it shakes a lot and that is the problem. Pleas any one help me to solve it. I have spend a lot time in it and this is what I got.
using UnityEngine;
using System.Collections;
public class CamRaycast : MonoBehaviour {
public Transform target;
public float speed = 1;
private RaycastHit hit3;
private RaycastHit hit4;
// Use this for initialization
void Start () {
}
//5 and -45 for backword raycast
// Update is called once per frame
void LateUpdate ()
{
transform.LookAt (target);//Tracks the player
Vector3 top = transform.TransformDirection (0f, 26f, 45f) * 10;//Forward Top
Debug.DrawRay (transform.position, top, Color.green);
Vector3 back = transform.TransformDirection (0f, 5f, -45f) * 10;//Forward Top
Debug.DrawRay (transform.position, back, Color.green);
if ((Physics.Raycast (transform.position, (back), out hit4) && hit4.distance < 2f && transform.position.y > 0.5f) ||
(Physics.Raycast (transform.position, (top), out hit3) && hit3.distance < 2f && transform.position.y > 0.5f)) {
transform.Translate (0f, -10f * Time.deltaTime, 0f);
}
else if ((Physics.Raycast (transform.position, (back), out hit4) && hit4.distance > 2 && transform.position.y < 2f)
|| (Physics.Raycast (transform.position, (top), out hit3) && hit3.distance > 2 && transform.position.y < 2f)) {
transform.Translate (0f, 10f * Time.deltaTime, 0f);
}
else if ((!Physics.Raycast (transform.position, (back), out hit4) && transform.position.y < 2f) ||
(!Physics.Raycast (transform.position, (back), out hit4) && transform.position.y < 2f))
{
transform.Translate (0f, 10f * Time.deltaTime, 0f);
}
}
}
This is what I wanted
– SimonClintonIv