- Home /
Please Help me converting this to C#
I have this little code which don't let the camera view be blocked, but this is in JS. Can you help me translate this to C#? I'm allways haveing a few errors in mine.
// IS VIEW BLOCKED?
var rotation:Quaternion = Quaternion.Euler(y, x, 0);
var hit : RaycastHit;
var trueTargetPosition : Vector3 = target.transform.position - Vector3(0,-targetHeight,0);
// Cast the line to check:
if (Physics.Linecast (trueTargetPosition, transform.position, hit)) {
// If so, shorten distance so camera is in front of object:
var tempDistance = Vector3.Distance (trueTargetPosition, hit.point) - 0.28;
// Finally, rePOSITION the CAMERA:
position = target.position - (rotation * Vector3.forward * tempDistance + Vector3(0,-targetHeight,0));
transform.position = position;
What are the "few errors"? C# is way more strict than Unityscript so don't expect it to be any easier.
Answer by Muuskii · Aug 12, 2012 at 04:46 PM
Woah! There is a lot here that doesn't make any sense.
void Update() //You didn't have any functions! I'm assuming you need this is in Update
{
// IS VIEW BLOCKED?
Transform target; //You never defined this!!
Quaternion rotation = Quaternion.Euler(y, x, 0); //where do these values come from?
RaycastHit hit;
Vector3 trueTargetPosition = target.transform.position - new Vector3(0,-targetHeight,0);//you needed "new"
// Cast the line to check:
//use Raycast, and you had the vectors backwards
if (Physics.Raycast (transform.position, trueTargetPosition - transform.position, out hit)) //out modifier is needed
{
//Vector3 tempDistance = Vector3.Distance (trueTargetPosition, hit.point) - 0.28; //no
//This is ALWAYS more helpful
Vector3 Difference = transform.position - hit.point; //Vector from hit.point
// If so, shorten distance so camera is in front of object:
Difference = Difference.normalized * (Difference.magnitude - 0.28f); //you needed the f
// Finally, rePOSITION the CAMERA:
/* position = target.position - (rotation * Vector3.forward * tempDistance + Vector3(0,-targetHeight,0));*/
//Where does "position" and "rotation" come from??
transform.position = target.position + Difference;
} //You were missing this bracket
}
}
Honestly some of the stuff in here makes it look like you barely even tried to code this in C# just saw a whole bunch of errors and posted the code without reading them.
I can't test this at the moment, please tell me how it goes from here.
Is that why the title of the question was "Help me convert this to C#"
Answer by hyderox · Aug 12, 2012 at 07:54 PM
Thank you for the help! As you can see I did not post my full script, only this part. I tried to translate it to C# but I had errors and I couldn't fix it.
Answer by Meltdown · Aug 12, 2012 at 08:25 PM
Use this code converter. It's not perfect but it does all the plumbing for you
Your answer
Follow this Question
Related Questions
How do I make a strategy camera go up and down? 1 Answer
Isometric camera skybox not working. 0 Answers
Connection between 2 scripts 1 Answer
How to mimic Unity Scene Gizmo? 2 Answers
Visibility of objects 2 Answers