Hi I am a newbie in Javascript, and was wondering a few things:
I modified the FPSWalker.js (attached to a Gameobj) to respond to a collision and switch to a 3rd person camera. EXAMPLE:
function OnControllerColliderHit(hit : ControllerColliderHit){
var collisionObj = hit.collider.gameObject.name ;
Debug.Log("character collided with " + collisionObj );
if (collisionObj == "wall")
{
hitWall = true;
}
if (collisionObj != "workArea"){
completedTxt.renderer.enabled = false;
//set the first person camera ON
firstPersonCamera.enabled = true;
thirdPersonCamera.enabled = false;
insideWrokArea = false;
//scriptXb.maximumX = 360F;
//Hide completion bar
healthGUI.enabled = false;
maskGUI.enabled = false;
}else{
//set the third person camera ON
firstPersonCamera.enabled = false;
thirdPersonCamera.enabled = true;
insideWrokArea = true;
varScript.maximumX = 0F;
if (taskCompleted == false){
//Show completion bar
healthGUI.enabled = true;
maskGUI.enabled = true;
}
}
}
This works fine but I am trying to access the:
public float minimumX = -360F;
public float maximumX = 360F;
From the MouseLook.cs script.
The plan was to change the parameters to 0 (therby preventing the player from spinning around) and then change it back to ±360 outside of the collision area.
I tried
// To access public variables and functions
// in another script attached to the same game object.
var scriptXa = GetComponent(MouseLook);
var scriptXa.minimumX = 0F;
// To access public variables and functions
// in another script attached to the same game object.
var scriptXb = GetComponent(MouseLook);
var scriptXb.maximumX = 0F;
But it gives me a “needs semicolon “;” at end” note though I have one there.
I appreciate any help and thanks in advance.
You’re mixing JS and C# there; you can’t write “0F” in JS. Just “0”.
–Eric
Thanks for the reply!
However, once I Impleneted the change I now get error BCE0043:Unexpected token;…
Thanks for the reply.
Could you paste the script again and say what line number? It’s difficult to help otherwise.
Cheers,
-Jon
Sure no problem:
From a modified FPSWalker.js script (in the standard assets)
// To access public variables and functions
// in another script attached to the same game object.
var scriptXa = GetComponent(MouseLook);
var scriptXa.minimumX = 0;
// To access public variables and functions
// in another script attached to the same game object.
var scriptXb = GetComponent(MouseLook);
var scriptXb.maximumX = 0;
Then the function that runs the variables in the FPSWalker.js script when the collision occurs:
function OnControllerColliderHit(hit : ControllerColliderHit){
var collisionObj = hit.collider.gameObject.name ;
Debug.Log("character collided with " + collisionObj );
if (collisionObj == "wall")
{
hitWall = true;
}
if (collisionObj != "workArea"){
completedTxt.renderer.enabled = false;
//set the first person camera ON
firstPersonCamera.enabled = true;
thirdPersonCamera.enabled = false;
insideWrokArea = false;
scriptXb.maximumX = 360;
//Hide completion bar
healthGUI.enabled = false;
maskGUI.enabled = false;
}else{
//set the third person camera ON
firstPersonCamera.enabled = false;
thirdPersonCamera.enabled = true;
insideWrokArea = true;
scriptXb.maximumX = 0;
if (taskCompleted == false){
//Show completion bar
healthGUI.enabled = true;
maskGUI.enabled = true;
}
}
}
Now here is the MouseLook.cs script (a standard camera script asset I believe.)
Lines 25-26
public float minimumX = -360F;
public float maximumX = 360F;
These 2 scripts are attached to the “First Person Controller” gameobject. I hope this is what you all needed?
You don’t need to use var twice in both cases, try this instead:
var scriptXa = GetComponent(MouseLook);
scriptXa.minimumX = 0;
scriptXa.maximumX = 0;
It’s the same component in both max/min x cases, get a reference to it once and use it for setting both property values. And then use just scriptXa in your collision function.
Thanks for the help HiggyB, however when I implement these changes I now get ("unknown identifier “MouseLook”) error messgage.
I did set the maximumX value to 0 at the top of the script
(also thanks for the spelling notice!)
I appreciate the help…
Ok, last problem: MouseLook is the name of an attached script and so when calling GetComponent() you must provide that name as a string:
var scriptXa = GetComponent("MouseLook");
Here is my entire FPSWalker.js script that toggles the min/max X values between 0/0 and -360/360 when you press “t”:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var scriptXa;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function Start() {
scriptXa = GetComponent("MouseLook");
scriptXa.minimumX = 0;
scriptXa.maximumX = 0;
}
function Update (){
if (Input.GetKeyDown("t")) {
if (scriptXa.minimumX == 0) {
scriptXa.minimumX = -360;
scriptXa.maximumX = 360;
} else {
scriptXa.minimumX = 0;
scriptXa.maximumX = 0;
}
}
}
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Notice that I made sure to initialize the reference to scriptXa (and the mix/max X values) on Start, that way I know that both scripts have had a chance to properly load an initialize, something I can’t guarantee if it’s initialized when first declared at the top of the script.
Thank you very much for your help! I think most of my problems came from the variables not initializing properly in the beginning so I placed them in my Start() function and then it was able to work!
Thanks again!!