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
1
Question by dragonking300 · Jan 02, 2017 at 03:57 PM · c#scripting problemscripting beginner

My camera script is having a lot of problems.....

What I want my script to do.

I want my script to be able to see my player object through a wall by 50% opacity when ever the player's view is obstructed by a wall

How I wanted my script work

  1. my camera would cast a ray facing then have the ray face the player(working)

  2. based on q and e input my camera would change rotation(working)

  3. if the ray hit a wall obstructing the path of the ray the wall in-front of it would turn transparent(Only works on some walls of the walls ;-;)

  4. once the ray stops colliding with the wall the wall returns to 0% transparent(not working at all)

problems I am facing

  1. Some walls turn 50% transparent and some don't(they have the exact same settings and the exact same tags and the exact same components

  2. The walls that do turn 50% transparent stay 50% transparent once they become transparent and don't change back

  3. "far" which is for my debug log for telling me a ray cast isn't hitting anything isn't triggering.

Could you help me with my script? I have to turn in my game for g.t by tommrow and this is one of the last things I need fixed ;-;

camera script

 using UnityEngine;
 using System.Collections;
 
 public class CameraControll : MonoBehaviour
 {
     Vector3 Campos;
     public Transform player;
     public GameObject Player;
     int count = 0;
     public float Distance;
     private GameObject block;
     
 
     public void Update()
     {
         Vector3 Direction = Player.transform.position - transform.position;
         RaycastHit hit;
         Ray LandingRay = new Ray(transform.position, Vector3.forward);
         Debug.DrawRay(transform.position, Direction * Distance);
         if (Physics.Raycast(LandingRay, out hit, Distance))
             
         {
             if(hit.collider.tag == "Wall")
             {
                 
                     Debug.Log("close");
                     block = hit.collider.gameObject;
                     block.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0.5f);
                 
              }
             else
             {  
                 Debug.Log("Far");
                  block = hit.collider.gameObject;
                 block.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 1);
             }
         }
 
         if (Input.GetKeyDown("q"))
         {
             count++;
         }
         else
         {
             if (Input.GetKeyDown("e"))
             {
                 count--;
             }
         }
         transform.position = Player.transform.position + Campos;
         transform.LookAt(player);
 
 
        
     }
 
     public void LateUpdate()
     {
         if (count == 0)
         {
             Campos = new Vector3(0f, 5f, -7f);
             Debug.Log(count);
         }
         else
         {
             if (count == 1)
             {
                 Campos = new Vector3(7f, 5f, 0f);
                 Debug.Log(count);
             }
             else
             {
                 if (count == 2)
                 {
                     Campos = new Vector3(0f, 5f, 7f);
                     Debug.Log(count);
                 }
                 else
                 {
                     if (count == 3)
                     {
                         Campos = new Vector3(-7f, 5f, 0);
                         Debug.Log(count);
                     }
                     else
                     {
                         if (count <= -1)
                         {
                             count = 3;
                         }
                     else
                         {
                             if (count <= 4)
                             {
                                 count = 0;
                             }
                         }
                     }
                 }
             }
         }
     }
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Creeper_Math · Jan 02, 2017 at 06:00 PM

I would "reset" all the gameobject's transparency to 100% DIRECTLY before you set the specific object's transparency that you need. This could be the following script (placed at the very top of the update script

 GameObject[] Walls = GameObject.FindGameObjectsWithTag("Wall");
 foreach (GameObject item in Walls) {
      item.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
 }

This resets all the walls before the camera sets the specific ones that it needs to be transparent

(This is the problem that was occuring) The Camera's "hit" tag would ALWAYS either be the player or a wall that NEEDED to be set to 50% transparency, so the following script would only come up when the camera has a direct line of sight to the player, in which the "hit" would always be the player

Script portion that would only occur if the camera had direct line of sight to player, and only affected the "hit" object of the player

 else
              {  
                  Debug.Log("Far");
                   block = hit.collider.gameObject;
                  block.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 1);
              }

As of the ray casting and only SOME walls changing problem

Your "Debug Ray" and your "Lading Ray" are hitting TWO different positions

 Ray LandingRay = new Ray(transform.position, Vector3.forward);
 // Landing Ray is showing directly forward of the camera
 
 
 Debug.DrawRay(transform.position, Direction * Distance);
 // Debug Ray is going between the player and the camera

You need to set them to the SAME ray, which could be the following code

 Ray LandingRay = new Ray(transform.position,player.transform.position - transform.position);

This way the script is going by the same ray that is being displayed by the Debug Ray

Comment
Add comment · Show 2 · 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 dragonking300 · Jan 02, 2017 at 07:51 PM 0
Share

wut, why the fuck is it when other people do it it suddenly works... Anyway, thank you for helping me get a working script but before you go could you explain to me how resetting the object's transparency to 100% makes the script work like bread n butter?

 GameObject[] Walls = GameObject.FindGameObjectsWithTag("Wall");
  foreach (GameObject item in Walls) {
       item.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
  }
avatar image Creeper_Math dragonking300 · Jan 03, 2017 at 09:47 PM 0
Share

The "Foreach" goes through every wall and resetting the transparency to 100%, so the walls that are no longer needed to be set to 50% transparency are turned to 100% transparency. The script basically changes the bool (100% or 50%) transparency into a "button", where every update, the button is pushed back up to 100% by the spring (This specific script is the spring), and only the ones that need to be pressed down are once more pressed and set to 50%.

avatar image
0

Answer by Hyrtwol · Jan 03, 2017 at 10:02 PM

sounds like at all-nighter ;) i would be careful with the material property "Returns the first instantiated Material assigned to the renderer." so setting the color can result in a new instance of the material on that render. prepare the wall mats in a Start() so you're sure they dont share materials.

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

275 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

Related Questions

Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer

How would I make a ListChangedEvent? 1 Answer

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

Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer

Spawning 3D Objects according to Data at JSON Matrix 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