Question by
Finn10232137 · Jul 11, 2017 at 01:10 PM ·
unity 2d
cant jump well on moving platform
hello guys my character cant jump well on moving platform. when i press Space on other ground, my character will jump smooth, the GetButton works very well and Sensitive, but here is the problem, when i stand on moving platform , my Space always cant work very well, i tried to debug it , i cant fix it , pls help me , thx lot.
here is my character C#
public GameObject Dog;
private float my_runspeed = 8;
private float my_jumpHeight = 800;
private Animator my_ani;
private bool Jumping = false;
private bool IsGround = false;
void Awake () {
my_body = GetComponent<Rigidbody2D> ();
my_ani = GetComponent<Animator>();
}
void FixedUpdate () {
MyJump();
MyMove ();
My_StateMachine ();
my_JumpState ();
}
void MyMove () {
float myxy = Input.GetAxis ("Horizontal");
my_body.velocity = new Vector2 (myxy * my_runspeed, my_body.velocity.y);
if (myxy != 0) {
float my_Scale_X = Mathf.Sign (my_body.velocity.x) == 1 ? 1 : -1;
Dog.transform.localScale = new Vector3 (my_Scale_X, 1, 1);
}
}
void MyJump () {
if (!IsGround != false || my_body.velocity.y != 0) {return;}
if (Input.GetButton("Jump")) {
Debug.Log(my_body.velocity);
my_body.AddForce (Vector3.up * my_jumpHeight); //Vector3.up = (0,1,0)
}
}
void My_StateMachine () {
my_ani.SetFloat ("Speed", Mathf.Abs (my_body.velocity.x));
my_ani.SetBool("Jumping", Jumping);
my_ani.SetBool("IsGround", IsGround);
}
void my_JumpState () {
if (my_body.velocity.y > 0.1f) {
Jumping = true;
IsGround = false;
}else if(Jumping){
Jumping = false;
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Ground" && coll.contacts[0].normal == Vector2.up) {
IsGround = true;
}
here is setParent
private Transform Target;
public GameObject Character;
public GameObject Platform;
void OnCollisionEnter2D(Collision2D coll) {
if(coll.gameObject.tag == "Player" && coll.contacts[0].normal == -Vector2.up)
{
Target = coll.gameObject.transform;
Target.SetParent(Platform.gameObject.transform);
}
}
void OnCollisionExit2D(Collision2D coll) {
if(coll.gameObject.tag == "Player")
{
Target = coll.gameObject.transform;
Target.SetParent(Character.gameObject.transform);
}
}
}
here is moving platform
public GameObject Platform;
public float min;
public float max;
private float Position_now;
private bool Move = true;
public float speed = 1f;
void Update() {
Position_now = Platform.gameObject.transform.localPosition.x;
if (Move == true) {
Platform.transform.localPosition += new Vector3(speed, 0, 0);
if (Position_now >= max) {
Move = false;
}
}
if (Move == false) {
Platform.transform.localPosition += new Vector3(-speed, 0, 0);
if (Position_now <= min) {
Move = true;
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612130012im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2 touch Destroy(gameObject); 2 Answers
Making an App Prototype without Coding 0 Answers
anyone here to help me with adobe animate 0 Answers
spawn position of ragdolls keep doubling 0 Answers