Question by 
               SimonClintonIv · Jun 22, 2016 at 05:27 PM · 
                cameracollisionraycast  
              
 
              What is wrong with the code I have written for the camera to avoid it from going through object
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);
         }
             
 
     }
}
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by SimonClintonIv · Jul 04, 2016 at 08:33 AM
using UnityEngine; using System.Collections;
public class CamRaycast : MonoBehaviour { public Transform target; private RaycastHit hit3; private RaycastHit hit4; //private RaycastHit hit5;
 // 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
     //Foward Top
     Vector3 top = transform.TransformDirection (0f, 26f, 45f) * 10;//Forward Top
     Debug.DrawRay (transform.position, top, Color.green);
     //Backward top
     Vector3 back = transform.TransformDirection (0f, 20f, -45f) * 10;//Forward Top
     Debug.DrawRay (transform.position, back, Color.green);
         
     //Foward Top
     if (Physics.Raycast (transform.position, (top), out hit3) && hit3.distance < 2 && transform.position.y > 0.5) {
         transform.Translate (0f, -10f * Time.deltaTime, 0f);
     }
     if (Physics.Raycast (transform.position, (top), out hit3) && hit3.distance > 2 && hit4.distance > 2 & transform.position.y < 2.3f) {
         transform.Translate (0f, 10f * Time.deltaTime, 0f);
     }
     if (!Physics.Raycast (transform.position, (top), out hit3) && hit4.distance > 2 && transform.position.y < 2.3f) {
         transform.Translate (0f, 10f * Time.deltaTime, 0f);
     }
     //Backward top
     if (Physics.Raycast (transform.position, (back), out hit4) && hit4.distance < 2 && transform.position.y > 0.5) {
         transform.Translate (0f, -20f * Time.deltaTime, 0f);
     }
     if (Physics.Raycast (transform.position, (back), out hit4) && hit4.distance > 2 && hit3.distance > 2 && transform.position.y < 2.3f) {
         transform.Translate (0f, 10f * Time.deltaTime, 0f);
     }
     if (!Physics.Raycast (transform.position, (back), out hit4) && hit3.distance > 2 && transform.position.y < 2.3f){
         transform.Translate (0f, 10f * Time.deltaTime, 0f);
     } 
 }
 void Center ()
 {
     RaycastHit hit;
     Vector3 forward = transform.TransformDirection (Vector3.forward) * 10;//FORWARD
     Debug.DrawRay (transform.position, forward, Color.green);
     if (Physics.Raycast (transform.position, (forward), out hit) && hit.collider.gameObject.name != "Player") 
     {
         transform.Translate (0f, 0f, 10f * Time.deltaTime);
     }
     if (Physics.Raycast (transform.position, (forward), out hit) && hit.collider.gameObject.name == "Player" && transform.position.y <= 2.4f && transform.position.z <= 5) 
     {
         transform.Translate (0f, 0f, -10f * Time.deltaTime);
     }
 }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                