纳金网

标题: 绳子效果实现代码 [打印本页]

作者: 晃晃    时间: 2011-8-12 10:42
标题: 绳子效果实现代码
采取的一种变通的办法来实现绳子效果,和官方有一个演示中用到的应该是类似的方法。下面是详细的介绍
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;
}
}
作者: 难骑    时间: 2011-8-12 21:34
提示: 作者被禁止或删除 内容自动屏蔽
作者: 晃晃    时间: 2011-12-30 18:22
此地無銀。。。

作者: 菜刀吻电线    时间: 2012-1-28 23:32
一帆风顺,二龙腾飞,三羊开泰,四季平安,五福临门,六六大顺,七星高照,八方来财,九九同心,十全十美。

作者: C.R.CAN    时间: 2012-2-19 23:29
水。。。

作者: 晃晃    时间: 2012-3-9 23:23
我看看就走,你们聊!

作者: 晃晃    时间: 2012-3-10 23:29
不错哦,顶一下......

作者: tc    时间: 2012-5-4 23:23
再看一看,再顶楼主

作者: tc    时间: 2012-6-14 23:25
很经典,很实用,学习了!

作者: tc    时间: 2012-6-29 23:25
凡系斑竹滴话要听;凡系朋友滴帖要顶!

作者: C.R.CAN    时间: 2012-9-4 23:27
楼主收集的可真全哦

作者: 菜刀吻电线    时间: 2012-10-14 23:27
已阵亡的 蝶 随 风 舞 说过  偶尔按一下 CTRL A 会发现 世界还有另一面

作者: 晃晃    时间: 2013-2-1 23:27
佩服,好多阿 ,哈哈





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5