RigidBody2D not working right. Help!
So I have a GUI button, named "Jump".
Now there's also a player character there.
I want when people tap/click on this "Jump" button, the character will jump. My idea is to attach UI button function onClick() on the "Jump" button, accessing the character rigidbody2D, and then addForce to it.
So I am adding this: in jumpCode function that attached to button:
using UnityEngine;
using System.Collections;
public class jumpCode : MonoBehaviour {
bool grounded = false;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
private MainCharacter MainChar;
void Start(){
MainChar = GameObject.Find ("Modern").GetComponent<Rigidbody2D>();
}
public void letsJump(){
if (grounded) {
grounded = false;
MainChar.AddForce (new Vector2 (0, jumpHeight));
}
}
}
I think I still have no luck in accessing the rigidbody2d component in my player character. I have tried to use GameObject to "drag & drop" the player gameobject to inspector, but still the AddForce couldn't be accessed.
Any help would be appreciated, as I have been browse through internet and find no clue. I am pretty new here, so Hello guys !
Note the player gameobject and button gameobject in separate hierarchy position.
Thanks!
$$anonymous$$ainChar needs to be of type Rigidbody2D, not $$anonymous$$ainCharacter
grounded stays false all the time
Your answer
Follow this Question
Related Questions
How to Stop Collider from Moving? 2 Answers
Arkanoid-like bounce troubles 1 Answer
Rigidbody follow player and maintain all physical properties 1 Answer
Stop movement on mouse interaction 1 Answer