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 /
avatar image
0
Question by dragonking300 · Mar 30, 2017 at 01:13 PM · c#camerascripting problemscripting beginner

One of the functions on my Camera script doesn't execute correctly.

I am trying to execute a function that rotates my camera based on where my player position is but when I try to use my function the camera starts to clip through the walls but when I don't use the function and replace my

 Vector3 back = player.transform.TransformDirection(-1 * Direction);

with

 Vector3 back = player.transform.TransformDirection(-1 * Vector3.forward);

The camera scales properly but doesn't rotate nor rotate around the player. Note that all the debug logs that are commented out are my attempts at the problem.

 My Main camera script
 
     using UnityEngine;
     using System.Collections;
     
     public class Alternate : MonoBehaviour
     {
         [SerializeField]
         private Transform target = null;[SerializeField]
         private float distance = 3.0f;[SerializeField]
         private float height = 1.0f;[SerializeField]
         private float damping = 5.0f;[SerializeField]
         public int count;
         public Transform player;
         public Vector3 CameraPositionOffset;
         private Vector3 Direction;
     
     
         [SerializeField]
         private Vector3 targetLookAtOffset; // allows offsetting of camera lookAt, very useful for low bumper heights
         [SerializeField]
         private float bumperDistanceCheck = 20f; // length of bumper ray
         [SerializeField]
         private float bumperCameraHeight = 1.0f; // adjust camera height while bumping
         [SerializeField]
         private Vector3 bumperRayOffset; // allows offset of the bumper ray from target origin
                                        
         private void Awake()
         {
             GetComponent<Camera>().transform.parent = player;
         }
         private void FixedUpdate()
         {
             GameObject thePlayer = GameObject.Find("Player");
             Dash DashScript = thePlayer.GetComponent<Dash>();
             Debug.Log(DashScript.CurrentWall);
             CameraOffset(); // does not execute properly and does nothing
             transform.LookAt(player);
             Vector3 wantedPosition = player.TransformPoint(0, height, -distance);
             // check to see if there is anything behind the target^
             RaycastHit hit;
             Vector3 back = player.transform.TransformDirection(-1 * Direction);
             Debug.DrawRay(player.TransformPoint(bumperRayOffset), back, Color.black, bumperDistanceCheck);
             // cast the bumper ray out from rear and check to see if there is anything behind
             if (Physics.Raycast(player.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
                 && hit.transform != target) // ignore ray-casts that hit the user. DR
             {
     
                 wantedPosition.x = hit.point.x + CameraPositionOffset.x;
                 wantedPosition.z = hit.point.z + CameraPositionOffset.z;
                 wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
             }
             transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
             Vector3 lookPosition = player.TransformPoint(targetLookAtOffset);
             if (Vector3.Distance(player.position, transform.position) <= 2)
             {
                 player.transform.parent.GetComponent<Renderer>().material.color = new Color(0, 0, 0, 0.7f);
             }
     
         }
     // this below is the function that doesn't work
         private void CameraOffset()
         {
             GameObject thePlayer = GameObject.Find("Player");
             Dash DashScript = thePlayer.GetComponent<Dash>();
             switch (DashScript.CurrentWall)
             {
                 case 1:
                     CameraPositionOffset = new Vector3(0, 0, -2); //This offset makes it so the camera doesn't //clip through walls do to delayed camera positioning updates
                     Direction = Vector3.forward * -1; //this is supposed to be determined which way my camera
 //raycast is coming from the player...
                     transform.eulerAngles = new Vector3(0, 180, 0); //Camera rotation
                     break;
                 case 2:
                     CameraPositionOffset = new Vector3(-2, 0, 0);
                     Direction = Vector3.left;
                     transform.eulerAngles = new Vector3(0, 0, -90);
                     break;
                 case 3:
                     CameraPositionOffset = new Vector3(0, 0, 2);
                     Direction = Vector3.forward;
                     transform.eulerAngles = new Vector3(0, 0, 0);
                     break;
                 case 4:
                     CameraPositionOffset = new Vector3(0, 0, 2);
                     Direction = Vector3.right;
                     transform.eulerAngles = new Vector3(0, 90, 0);
                     break;
                 case 5:
                     CameraPositionOffset = new Vector3(0, -2, 0);
                     Direction = Vector3.down;
                     transform.eulerAngles = new Vector3(0, 0, 0);
                     break;
                 case 6:
                     CameraPositionOffset = new Vector3(0, 2, 0);
                     Direction = Vector3.up;
                     transform.eulerAngles = new Vector3(0, 0, 0);
                     break;
                 default:
                     Debug.LogError("CurrentWall parameter needs ajusting m8");
                     break;
                     
             }
         }
     }

2nd or secondary camera script

   using UnityEngine;
     using System.Collections;
     
     //Make the handles more thick
     
     public class Dash : MonoBehaviour
     {
         public GameObject Previouscollided;
         private GameObject collided;
         public LayerMask Wall;
         Vector3 newPosition;
 //Below is all the angles the cameras will face from
  public GameObject Wall1;
         public GameObject Wall2;
         public GameObject Wall3;
         public GameObject Wall4;
         public GameObject Floor;
         public GameObject Ceiling;
         public GameObject RecentWall;
 
 public int speed;
         [SerializeField]
         private int DistanceOfRayCast;
         [SerializeField]
         private int CornerDistanceOfRayCast;
 //W.I.P code(don't mind v)
 private bool UpperCornerView = false;
 private bool LowerCornerView = false;
 //W.I.P code(don't mind ^)`enter code here`
         private int CurrentCorner;
         private Vector3 WallThicknessOffset;
         public int CurrentWall;
         //Remember line 22
     
         void Start()
         {
             newPosition = transform.position;
             CurrentWall = 3;
         }
     
 
         void Update()
         {
            // Debug.Log("CurrentCorner = " + CurrentCorner);
     
             if (Input.GetKeyDown("w"))
             {
                 transform.Rotate(1, 0, 0);
             }
             if (Input.GetKeyDown("s"))
             {
                 transform.Rotate(-1, 0, 0);
             }
     
             //Debug.Log(UpperCornerView);
     
             if (Input.GetMouseButtonDown(0))
             {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 if (Physics.Raycast(ray, out hit, Wall.value))
                 {
                     UpperCornerView = false;
                     LowerCornerView = false;
                     CurrentCorner = 0;
     
                     collided = hit.collider.gameObject;
                     RecentWall = collided;
                     //Debug.Log(collided);
                     //Cameraposition();
                     collided.layer = LayerMask.NameToLayer("Ignore Raycast");
                     //( ͡° ͜ʖ ͡°)
                     newPosition = hit.point;
                     //Debug.Log("test");
     
     
                     //This part of the Script handles how the camera handles interactions with corners
                     RaycastHit HitUp;
                     Ray UpRay = new Ray(newPosition, Vector3.up);
                     Debug.DrawRay(newPosition, Vector3.up * DistanceOfRayCast, Color.red, 10);
     
                     RaycastHit HitDown;
                     Ray DownRay = new Ray(newPosition, Vector3.forward);
                     Debug.DrawRay(newPosition, Vector3.down * DistanceOfRayCast, Color.red, 10);
     
                     RaycastHit HitLeft;
                     Ray LeftRay = new Ray(newPosition, Vector3.left);
                     Debug.DrawRay(newPosition, Vector3.left * CornerDistanceOfRayCast, Color.red, 10);
     
                     RaycastHit HitRight;
                     Ray RightRay = new Ray(newPosition, Vector3.right);
                     Debug.DrawRay(newPosition, Vector3.right * CornerDistanceOfRayCast, Color.red, 10);
     
                     RaycastHit HitForward;
                     Ray ForwardRay = new Ray(newPosition, Vector3.right);
                     Debug.DrawRay(newPosition, Vector3.forward * CornerDistanceOfRayCast, Color.red, 10);
     
                     RaycastHit HitBackward;
                     Ray BackwardRay = new Ray(newPosition, Vector3.up);
                     Debug.DrawRay(newPosition, Vector3.back * CornerDistanceOfRayCast, Color.red, 10);
     
                     if (Physics.Raycast(UpRay, out HitUp, DistanceOfRayCast))
                     {
                         if (HitUp.distance <= 2)
                         {
                             UpperCornerView = true;
                         }
                     }
     
                     if (Physics.Raycast(DownRay, out HitDown, DistanceOfRayCast))
                     {
                         if (HitDown.distance <= 2)
                         {
                             LowerCornerView = true;
                         }
                     }
     
                     if (Physics.Raycast(LeftRay, out HitLeft, CornerDistanceOfRayCast) && Physics.Raycast(ForwardRay, out HitForward, CornerDistanceOfRayCast))
                     {
                         if (HitDown.distance <= 2)
                         {
                             CurrentCorner = 1;
                         }
                     }
     
                     if (Physics.Raycast(LeftRay, out HitLeft, CornerDistanceOfRayCast) && Physics.Raycast(BackwardRay, out HitBackward, CornerDistanceOfRayCast))
                     {
                         if (HitDown.distance <= 2)
                         {
                             CurrentCorner = 2;
                         }
                     }
     
                     if (Physics.Raycast(RightRay, out HitRight, CornerDistanceOfRayCast) && Physics.Raycast(ForwardRay, out HitForward, CornerDistanceOfRayCast))
                     {
                         if (HitDown.distance <= 2)
                         {
                             CurrentCorner = 3;
                         }
                     }
     
                     if (Physics.Raycast(RightRay, out HitLeft, CornerDistanceOfRayCast) && Physics.Raycast(BackwardRay, out HitBackward, CornerDistanceOfRayCast))
                     {
                         if (HitDown.distance <= 2)
                         {
                             CurrentCorner = 4;
                         }
                     }
     
     
                     Previouscollided.layer = LayerMask.NameToLayer("Wall");
                     Previouscollided = hit.collider.gameObject;
                     //Debug.Log(Previouscollided);
                 }
             }
     
             if (collided == Wall1)
             {
                 CurrentWall = 1;
                 if (CurrentCorner == 1)
                 {
                     WallThicknessOffset = new Vector3(-2, 0, -2);
                 }
                 else
                 if (CurrentCorner == 4)
                 {
                     WallThicknessOffset = new Vector3(2, 0, 2);
                 }
                 else
                     WallThicknessOffset = new Vector3(0, 0, -2);
                 transform.position = Vector3.Lerp(transform.position, newPosition + WallThicknessOffset, speed * Time.deltaTime);
             }
     
             if (collided == Wall2)
             {
                 CurrentWall = 2;
                 if (CurrentCorner == 2)
                 {
                     WallThicknessOffset = new Vector3(-2, 0, 2);
                 }
                 else
                 if (CurrentCorner == 3)
                 {
                     WallThicknessOffset = new Vector3(2, 0, 2);
                 }
                 else
                     WallThicknessOffset = new Vector3(-2, 0, 0);
                 transform.position = Vector3.Lerp(transform.position, newPosition + WallThicknessOffset, speed * Time.deltaTime);
             }
     
             if (collided == Wall3)
             {
                 CurrentWall = 3;
                 if (CurrentCorner == 3)
                 {
                     WallThicknessOffset = new Vector3(2, 0, 2);
                 }
                 else
                 if (CurrentCorner == 4)
                 {
                     WallThicknessOffset = new Vector3(2, 0, -2);
                 }
                 else
                     WallThicknessOffset = new Vector3(0, 0, 2);
                 transform.position = Vector3.Lerp(transform.position, newPosition + WallThicknessOffset, speed * Time.deltaTime);
             }
     
             if (collided == Wall4)
             {
                 CurrentWall = 4;
                 if (CurrentCorner == 4)
                 {
                     WallThicknessOffset = new Vector3(2, 0, -2);
                 }
                 else
                 if (CurrentCorner == 1)
                 {
                     WallThicknessOffset = new Vector3(-2, 0, -2);
                 }
                 else
                     WallThicknessOffset = new Vector3(2, 0, 0);
                 transform.position = Vector3.Lerp(transform.position, newPosition + WallThicknessOffset, speed * Time.deltaTime);
             }
     
             if (collided == Ceiling)
             {
                 CurrentWall = 5;
             }
     
             if (collided == Floor)
             {
                 CurrentWall = 6;
                 transform.position = Vector3.Lerp(transform.position, newPosition - new Vector3(0, 0, 0), speed * Time.deltaTime);
             }
     
             if (UpperCornerView == true)
             {
     
             }
         }
     }
     /*  public void Cameraposition()
       {
           if (collided == Wall1)
           {
               GameObject theCamera = GameObject.Find("Main Camera");
               Alternate cameraScript = theCamera.GetComponent<Alternate>();
               //cameraScript.Campos = cameraScript.PreviousCampos;
               cameraScript.count = 0;
           }
           if (collided == Wall2)
           {
               GameObject theCamera = GameObject.Find("Main Camera");
               Alternate cameraScript = theCamera.GetComponent<Alternate>();
               // cameraScript.Campos = cameraScript.PreviousCampos;
               cameraScript.count = 1;
           }
           if (collided == Wall3)
           {
               GameObject theCamera = GameObject.Find("Main Camera");
               Alternate cameraScript = theCamera.GetComponent<Alternate>();
               //cameraScript.Campos = cameraScript.PreviousCampos;
               cameraScript.count = 2;
           }
           if (collided == Wall4)
           {
               GameObject theCamera = GameObject.Find("Main Camera");
               Alternate cameraScript = theCamera.GetComponent<Alternate>();
               // cameraScript.Campos = cameraScript.PreviousCampos;
               cameraScript.count = 3;
           }
           if (collided == Floor)
           {
               GameObject theCamera = GameObject.Find("Main Camera");
               Alternate cameraScript = theCamera.GetComponent<Alternate>();
               //  cameraScript.Campos = cameraScript.PreviousCampos;
               cameraScript.count = 4;
           }
           if (collided == Ceiling)
           {
               GameObject theCamera = GameObject.Find("Main Camera");
               Alternate cameraScript = theCamera.GetComponent<Alternate>();
               //cameraScript.Campos = cameraScript.PreviousCampos;
               cameraScript.count = 5;
           }
     }*/












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 dragonking300 · Mar 30, 2017 at 06:40 PM

So apparently I forgot that transform.lookat(player) makes it so rotations can't change and direction was working...

Comment
Add comment · 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

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

317 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 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 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 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

Rotatearaound a moving object 1 Answer

"Follow Target" script with a prefab (C#) Help me please! 1 Answer

UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers

delay "after mouseclick" 0 Answers

Problem with a Unity Scripting example from the scripting manual! 0 Answers


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