I’m fairly new to coding and have been testing how strong I am by trying to build a 2D platformer. My code might be messy but instead of cleaning it up or anything I’d rather you would fix the bit I got wrong. So I have two scripts, my first is the character control:
#pragma strict
var Respawn : Rigidbody2D;
var JumpHeight : float;
var Up : KeyCode;
var Down : KeyCode;
var Left : KeyCode;
var Right : KeyCode;
static var Grounded : int = 1;
function Update () {
if (Input.GetKey(Up) && Grounded == 1){
rigidbody2D.AddForce (new Vector2 (0, JumpHeight));
Grounded = 0;
}
else if (Input.GetKey(Left)){
rigidbody2D.velocity.x = -10;
}
else if (Input.GetKey(Right)){
rigidbody2D.velocity.x = 10;
}
else{
rigidbody2D.velocity.x = 0;
}
}
function OnCollisionEnter2D (GroundCol : Collision2D){
if (Grounded==0 && GroundCol.collider.tag == "Ground"){
Grounded = 1;
}
else if (Grounded==0 && GroundCol.collider.tag == "Wall"){
Grounded = 0;
}
else if (GroundCol.collider.tag == "OutOfBounds"){
transform.position.x = Respawn.position.x;
transform.position.y = Respawn.position.y;
var CamRestart = "reset";
FollowCamera.RestartCam (CamRestart);
}
}
And my second one is a camera control script:
var cameraTarget : GameObject;
var player : GameObject;
var CameraReset : Rigidbody2D;
var CameraR : Rigidbody2D;
var smoothTime : float = 1;
var cameraFollowX : boolean = true;
var cameraFollowY : boolean = true;
var cameraFollowHeight : boolean = false;
var cameraHeight : float = 2.5;
var velocity : Vector2;
private var thisTransform : Transform;
static var CamR : int = 0;
function Start ()
{
thisTransform = transform;
}
function Update (){
if (cameraFollowX){
thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
}
if (cameraFollowY){
thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
}
if (!cameraFollowX && cameraFollowHeight){
camera.transform.position.y = cameraHeight;
}
}
function RestartCam(CamRestart : String){
if (CamRestart == "reset"){
CameraReset.position.x = CameraReset.position.x;
CameraReset.position.y = CameraReset.position.y;
}
}
My problem is as I try to call the “RestartCam” function found just about unity shows me this message: “Assets/CharacterControll.js(38,30): BCE0020: An instance of type ‘FollowCamera’ is required to access non static member ‘RestartCam’.”
Any ideas?