firstly i hope all of you are having a good 2011!!
Im currently having a little difficulty writing a c# script that should during update fire a ray from the center of the camera forward and report the tag of the object that it hits.
This is the script as it currently stands:
void Update()
{
//The objective of this function is to cast a ray from the cameras center to any object that
//is in the space between the camera and the player this a test script as the functionality
//should be moved to the camrea controler script
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10))
{
Debug.Log("object hit " + otherObject.name);
}
}
What’s ‘otherObject’? I don’t see that defined anywhere in your code. (In any case, what you’ll probably want to do is use the overload of Raycast() that fills out a RaycastHit struct if there’s an intersection; you can then get the tag of the object that was hit from this struct.)
Thanks for the responce is this how you would get RaycastHit to work?
void Update()
{
//The objective of this function is to cast a ray from the cameras center to any object that
//is in the space between the camera and the player this a test script as the functionality
//should be moved to the camrea controler script
Vector3 fwd = transform.forward;
RaycastHit hit;
if (Physics.Raycast(transform.position, fwd, ref hit, 10))
{
Debug.Log("object hit " + hit);
}
}
That looks right (the only questionable bit is submitting ‘hit’ as an argument to Debug.Log()). In any case, the info you’re interested in can be accessed via the RaycastHit struct’s member fields/properties (check the docs for details).
// Update is called once per frame
void Update()
{
//The objective of this function is to cast a ray from the cameras center to any object that
//is in the space between the camera and the player this a test script as the functionality
//should be moved to the camrea controler script
Vector3 fwd = transform.forward;
RaycastHit hit;
if (Physics.Raycast(transform.position, fwd, ref hit, 10))
{
//at this point i now would like to identify what the above raycast has hit in the console
Debug.Log("object hit " + hit);
}
}
i get the following errors:
1.) Assets/Scripts/Camera Work/S_CW_Camera_Raycaster.cs(39,58): error CS0165: Use of unassigned local variable `hit’
2.) Assets/Scripts/Camera Work/S_CW_Camera_Raycaster.cs(39,21): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit, float)’ has some invalid arguments
Those actually aren’t errors you get when you run the script - those are compiler errors.
Anyway, it looks like there might be an error in the docs for Raycast() - the ‘hit’ parameter should be an ‘out’ parameter, not a ‘ref’ parameter. (That’s what the errors are telling you.)
After thinking that i had the issue sovled i have run into another issue, heres the code:
void Update()
{
//The objective of this function is to cast a ray from the cameras center to any object that
//is in the space between the camera and the player this a test script as the functionality
//should be moved to the camrea controler script
Vector3 fwd = transform.forward;
RaycastHit hit;
if (Physics.Raycast(transform.position, fwd, out hit, 10))
{
//at this point i now would like to identify what the above raycast has hit in the console
Debug.Log("object hit " + hit.collider.tag);
renderer.material.shader = Shader.Find("Transparent/Diffuse");
renderer.material.color.a = 0.3F;
/*
//we should now check what tag we have hit so we can perform the correct action
if (hit.collider.tag = Envroment_Objects)
{
Debug.Log("We need to remove " + hit.collider.tag);
}
*/
}
}
i get the following compiler error:
Assets/Scripts/Camera Work/S_CW_Camera_Raycaster.cs(45,31): error CS1612: Cannot modify a value type return value of `UnityEngine.Material.color’. Consider storing the value in a temporary variable
im trying to have the object that has been tagger by a ray turn transparent
If so, the problem is that ‘color’ is a property that returns a Color object, which is a temporary (and therefore unmodifiable) copy of the actual color. Here’s one way to get around this (untested):
Color color = renderer.material.color;
color.a = .3f;
renderer.material.color = color;
the code runs and has now compiler errors but i not get the following error:
MissingComponentException: There is no ‘Renderer’ attached to the “3rd_Person_Camera” game object, but a script is trying to access it.
You probably need to add a Renderer to the game object “3rd_Person_Camera”. Or your script needs to check if the component is attached before using it.
S_CW_Camera_Raycaster.Update () (at Assets/Scripts/Camera Work/S_CW_Camera_Raycaster.cs:44)
i think that my function is reading the camera rather than the game object:
// Update is called once per frame
void Update()
{
//The objective of this function is to cast a ray from the cameras center to any object that
//is in the space between the camera and the player this a test script as the functionality
//should be moved to the camrea controler script
Vector3 fwd = transform.forward;
RaycastHit hit;
if (Physics.Raycast(transform.position, fwd, out hit, 10))
{
//at this point i now would like to identify what the above raycast has hit in the console
Debug.Log("object hit " + hit.collider.tag);
renderer.material.shader = Shader.Find("Transparent/Diffuse");
Color color = renderer.material.color;
color.a = 0.3f;
renderer.material.color = color;
}
}
Did you mean, ‘The code runs and has no compiler errors, but I now get the following error’? (Might seem nitpicky, but the typos make the intended meaning a little ambiguous.)
As for the error, does the object to which that script is attached have any type of renderer component attached to it?
I’d be a little surprised if the game object had a renderer attached and you were still getting that error. I’d double-check to make sure both components are actually attached to the same game object, and maybe add something like this:
if (!renderer) {
Debug.Log("No renderer");
}
To see if the renderer is actually missing (I think the ! operator will work in that context…).
it seems that the object does not have a renderer, but in the inspector the object has has a mesh renderer do i need to add one in script?
Current code:
void Update()
{
//The objective of this function is to cast a ray from the cameras center to any object that
//is in the space between the camera and the player this a test script as the functionality
//should be moved to the camrea controler script
Vector3 fwd = transform.forward;
RaycastHit hit;
if (Physics.Raycast(transform.position, fwd, out hit, 10))
{
//at this point i now would like to identify what the above raycast has hit in the console
Debug.Log("object hit " + hit.collider.tag);
if (!renderer)
{
Debug.Log("No renderer");
}
renderer.material.shader = Shader.Find("Transparent/Diffuse");
Color color = renderer.material.color;
color.a = 0.3f;
renderer.material.color = color;
}
}
I’m not sure what the problem is. Could you post a screenshot (or set of screenshots) showing the inspector for the object in question? (If you get really stuck, you could create a small project demonstrating the problem and then post it as a Unity package.)
I think i may be missing somthing simple, but how do i talk directly to the game object that my raycaster hits (as i think this is the issue that i’m having, i have the data on what the raycaster hits but i havent figured out how to talk to that object shader to change it to a transparent one)?
Right, the ‘render’ property is associated with the object with which the script is associated, which is likely not the same as whatever object has been detected using a raycast. To access the object intersected using a raycast, use the overload of Raycast() that accepts a RaycastHit object as a parameter, and then access the game object via the RaycastHit object (see the docs for Raycast and RaycastHit for details).