Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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);
     }
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SimonClintonIv · Jul 04, 2016 at 08:33 AM 0
Share

This is what I wanted

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make a third person camera collision script? 2 Answers

Looking at closest visible object with tag 0 Answers

RayCast not working , why ??? 0 Answers

RayCast not working with MeshCollider - Unity's roller ball collision works 1 Answer

Raycast ignores objects with colliders 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges