I tried to create a climb script and found some similar code. I would like to climb with my player on a ladder. I came up with the following:
bool canClimb = false; float speed = 1;
GameObject player; // Reference to the player GameObject.
void Awake () {
// Setting up the references.
player = GameObject.FindGameObjectWithTag ("Player");
}
void OnTriggerEnter2D(Collider2D other) { // send in the other collider (should always work)
if (other.gameObject.tag == "Player") { // used a tag to ID collider as player
canClimb = true;
player.rigidbody.useGravity = false;
}
}
void OnTriggerExit2D(Collider2D other) { // send in the other collider (should always work)
if(other.gameObject == "Player"){
canClimb = false;
other.attachedRigidbody.useGravity = true;
}
}
void Update () {
if(canClimb == true){
if(Input.GetKey(KeyCode.Z)){
player.transform.Translate (Vector3(0,1,0) * Time.deltaTime*speed);
}
if(Input.GetKey(KeyCode.S)){
player.transform.Translate (Vector3(0,-1,0) * Time.deltaTime*speed);
}
}
}
The above code will give a red mark on the useGravity
in the line of player.rigidbody.useGravity
. I doesn't know how to fix this.
object.rigidbody
– jhocking Mar 25 at 20:09