- Home /
3rd Person Camera & Controller C#
Hi
Does anybody know if I can get the 3rd person camera and controllers that ship with unity in c#. Eventually I will create my own but I would like to use the one that ships with unity for a quick prototype. I've made an on screen thumbstick that holds its own axis values so I need to modify the script so that it takes my thumbstick axis values instead.
Answer by aldonaletto · Jul 15, 2011 at 03:59 AM
I don't believe someone have translated both scripts to C# - they are too complex, and the translation would be a typical too pain, no gain case. But you can modify the ThirdPersonController.js script to use your thumbstick:
1- Create a new folder at Assets and place your C# script there - this enables it to "see" the ThirdPersonController script.
2- Modify your script to copy the axis values to two static vars located at the ThirdPersonController script:
ThirdPersonController.axisH and ThirdPersonController.axisV
3- Add the two lines below at the beginning of the ThirdPersonController script:
static var axisH: float = 0;
static var axisV: float = 0;
4- Locate the two lines below:
var v = Input.GetAxisRaw("Vertical");
var h = Input.GetAxisRaw("Horizontal");
and change them to:
var v = axisV;
var h = axisH;
This is enough to do the basic movements with your thumbstick, but doesn't include the Shift or Jump keys - you must use the keyboard shift and your thumbstick to make the character run, and the Jump key at the keyboard to make it jump. If you have equivalents to these commands in your thumbstick, use the same logic to implement them in the ThirdPersonController (search for Input.GetKey for the shift, Input.GetButton for the jump).
Hi aldonaletto. I understood which values to change but I think I got confused with which way round my unityscript and c# scripts could communicate. I thought it was the other way round so thanks for that :)
Just a quick question. Shouldn't I need to put the class name before the axis values to access them and do they have to be static for them to be accessed?
1- Communication between JS and C# is a problem because they do not "see" each other at compiler time. Once compiled to CIL (the .NET Commom Intermediate Language), however, their static variables become visible to other scripts not compiled yet (maybe static functions too, but I'm not sure). Since you've placed your C# script in a non-standard folder, it will be compiled last, what enables it to "see" the static variables of all already compiled scripts - no matter if C# or JS (or Boo, if someone in this planet use it). But the already compiled scripts don't see your script, so this is a one-way communication line.
2- Static variables can be accessed as scriptName.variableName by other scripts. Non-static variables of other scripts should be accessed via GetComponent, but I'm not sure if it would work in this case (mixed C# and JS), even in the right compiling order. You can access these variables in your C# script doing this:
ThirdPersonController.axisH = myHAxis;
ThirdPersonController.axisV = myVAxis;
Thanks again. I appreciate your detailed answers. $$anonymous$$akes sense to me now. I think developers who sell in the asset store should be encouraged to make their scripts available in multiple languages, otherwise I can see this causing problems.