- 最后登录
- 2018-6-29
- 注册时间
- 2011-7-1
- 阅读权限
- 20
- 积分
- 359

- 纳金币
- 335582
- 精华
- 0
|
采取的一种变通的办法来实现绳子效果,和官方有一个演示中用到的应该是类似的方法。下面是详细的介绍
The rope is made using character joints, except for the top (which has its mesh unrendered) which uses a hinge joint. All the segments are triggers, rigidbodies using gravity and have a "ladder" tag on each segment except for the top and bottom (The reason for this is shown in the video as to what happens when the bottom segment is tagged ladder). The player climbs the rope by pressing the W or S key.
Everything is done in the player's script. What we don't know is if the problem is in the player, the way the rope is made or both. An alternative method to making the rope or rope climb/swing script is also welcome.
The player does not use a character controller. Below is a snippet of the code. All the climbing
variable does is keep the player from moving left and right as if they were on the ground.
function OnTriggerStay ( other : Collider)
{
if (other.gameObject.tag == "ladder")
{
if (Input.GetKey("w"))
{
rigidbody.velocity.y=0;
rigidbody.velocity.x=0;
rigidbody.useGravity=false;
transform.Translate ( Vector2(0, ClimbSpeed * Time.deltaTime));
DoubleJump = JumpReset;
Climbing = true;
transform.parent=other.transform;
}
if (Input.GetKey("s"))
{
rigidbody.velocity.y=0;
rigidbody.velocity.x=0;
rigidbody.useGravity=false;
transform.Translate (Vector2(0, -ClimbSpeed * Time.deltaTime));
DoubleJump = JumpReset;
Climbing = true;
transform.parent=other.transform;
}
if (transform.parent.tag == "ladder")
{
transform.localPosition.x = 0;
transform.rotation = transform.parent.rotation;
transform.parent.gameObject.rigidbody.AddRelativeForce(Vector3.right * Input.GetAxis("Horizontal") * 1, ForceMode.VelocityChange);
if(Input.GetAxis("Horizontal"))
{
rigidbody.velocity.x=0;
transform.localPosition.x=0;
}
}
}
if (other.gameObject.tag == "Mesh")
{
if (Input.GetKey("w"))
{
//rigidbody.isKinematic = true;
rigidbody.velocity.y=0;
rigidbody.velocity.x=0;
rigidbody.useGravity=false;
transform.Translate ( Vector2(0, ClimbSpeed * Time.deltaTime));
DoubleJump = JumpReset;
}
if (Input.GetKey("s"))
{
//rigidbody.isKinematic = true;
rigidbody.velocity.y=0;
rigidbody.velocity.x=0;
rigidbody.useGravity=false;
transform.Translate (Vector2(0, -ClimbSpeed * Time.deltaTime));
DoubleJump = JumpReset;
}
if(Input.GetButtonDown("Jump"))
{
rigidbody.useGravity=true;
rigidbody.velocity.y=jump;
DoubleJump -= 1;
transform.rotation=Quaternion.identity;
}
}
}
function OnTriggerExit ( other : Collider)
{
if (other.gameObject.tag == "ladder")
{
//rigidbody.isKinematic = false;
rigidbody.useGravity=true;
transform.rotation = Quaternion.identity;
Climbing = false;
transform.parent = null;
}
if (other.gameObject.tag == "Mesh")
{
//rigidbody.isKinematic = false;
rigidbody.useGravity=true;
transform.rotation = Quaternion.identity;
Climbing = false;
transform.parent = null;
}
} |
|