RT 小弟小白 用的2d playground的移动和跳跃脚本 想实现跳跃后重力反转 然后掉到天花板上去(接触天花板时角色能自动反转的功能能告诉就更好啦),再次跳跃后又重复反转,类似以前ios的游戏gentlemen的那个效果,谢谢各位。跳跃脚本如下
using UnityEngine;
using System.Collections;
[AddComponentMenu("Playground/Movement/Jump")]
[RequireComponent(typeof(Rigidbody2D))]
public class Jump : Physics2DObject
{
[Header("Jump setup")]
// the key used to activate the push
public KeyCode key = KeyCode.Space;
// strength of the push
public float jumpStrength = 10f;
[Header("Ground setup")]
//if the object collides with another object tagged as this, it can jump again
public string groundTag = "Ground";
//this determines if the script has to check for when the player touches the ground to enable him to jump again
//if not, the player can jump even while in the air
public bool checkGround = true;
private bool canJump = true;
// Read the input from the player
void Update()
{
if(canJump
&& Input.GetKeyDown(key))
{
// Apply an instantaneous upwards force
rigidbody2D.AddForce(Vector2.up * jumpStrength, ForceMode2D.Impulse);
canJump = !checkGround;
}
}
private void OnCollisionEnter2D(Collision2D collisionData)
{
if(checkGround
&& collisionData.gameObject.CompareTag(groundTag))
{
canJump = true;
}
}
}
using UnityEngine;
using System.Collections;
[AddComponentMenu("Playground/Movement/Jump")]
[RequireComponent(typeof(Rigidbody2D))]
public class Jump : Physics2DObject
{
[Header("Jump setup")]
// the key used to activate the push
public KeyCode key = KeyCode.Space;
// strength of the push
public float jumpStrength = 10f;
[Header("Ground setup")]
//if the object collides with another object tagged as this, it can jump again
public string groundTag = "Ground";
//this determines if the script has to check for when the player touches the ground to enable him to jump again
//if not, the player can jump even while in the air
public bool checkGround = true;
private bool canJump = true;
// Read the input from the player
void Update()
{
if(canJump
&& Input.GetKeyDown(key))
{
// Apply an instantaneous upwards force
rigidbody2D.AddForce(Vector2.up * jumpStrength, ForceMode2D.Impulse);
canJump = !checkGround;
}
}
private void OnCollisionEnter2D(Collision2D collisionData)
{
if(checkGround
&& collisionData.gameObject.CompareTag(groundTag))
{
canJump = true;
}
}
}