查看: 1631|回复: 0
打印 上一主题 下一主题

简单的汽车脚本

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-1-31 11:02:59 |只看该作者 |倒序浏览
var wheelFL : Transform;      

var wheelFR : Transform;      

var wheelRL : Transform;      

var wheelRR : Transform;      

var oFL : Transform;   

var oFR : Transform;   

  

var rearWheel1 : WheelCollider;      

var rearWheel2 : WheelCollider;      

var frontWheel1 : WheelCollider;      

var frontWheel2 : WheelCollider;      

  

var steer_max = 20;      

var motor_max = 70;      

var brake_max = 150;      

  

private var steer = 0;      

private var forward = 0;      

private var back = 0;      

private var motor = 0;      

private var brake = 0;      

private var reverse = false;      

private var speed = 0;      

  

private var body;   

public var  mskin : GUISkin;   

private var oldmovie = false;   

  

function Start()   

{   

    rigidbody.centerOfMass = Vector3(0, -0.57, 0);      

}   

function Update()   

{   

  

}   

function changecolor(mcolor:Color)   

{   

    for(i in GameObject.FindGameObjectsWithTag("carBody"))   

        for(j in i.renderer.materials)   

            if(j.shader.name=="Reflective/Specular")   

                j.SetColor("_Color",mcolor);      

}   

  

function FixedUpdate ()      

{      

    speed = rigidbody.velocity.sqrMagnitude;      

    steer = Mathf.Clamp(Input.GetAxis("Horizontal"), -1, 1);      

    forward = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);      

    back = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);      

     

    if(speed == 0)      

    {      

        if(back > 0) { reverse = true; }      

        if(forward > 0) { reverse = false; }      

    }      

     

    if(reverse)      

    {      

        motor = -1 * back;      

        brake = forward;      

    }      

    else      

    {      

        motor = forward;      

        brake = back;      

    }      

  

    if(speed<500)   

    {   

        rearWheel1.motorTorque = motor_max * motor;      

        rearWheel2.motorTorque = motor_max * motor;      

        frontWheel1.motorTorque = motor_max * motor;      

        frontWheel2.motorTorque = motor_max * motor;      

    }   

    else  

    {   

        rearWheel1.motorTorque = 0;      

        rearWheel2.motorTorque = 0;      

        frontWheel1.motorTorque = 0;      

        frontWheel2.motorTorque = 0;      

    }   

    rearWheel1.brakeTorque = brake_max * brake;      

    rearWheel2.brakeTorque = brake_max * brake;      

    frontWheel1.brakeTorque = brake_max * brake;      

    frontWheel2.brakeTorque = brake_max * brake;      

     

    frontWheel1.steerAngle = steer_max * steer;      

    frontWheel2.steerAngle = steer_max * steer;      

      

    var mRotation : Quaternion;      

    mRotation=Quaternion.Euler( 0 ,20* steer, 0) * Quaternion.identity;      

      

    oFL.localRotation= mRotation;      

    oFR.localRotation= mRotation;      

     

    wheelFL.Rotate(frontWheel1.rpm * 6 * Time.deltaTime,0, 0 );      

    wheelFR.Rotate(frontWheel1.rpm * 6 * Time.deltaTime,0, 0 );      

    wheelRL.Rotate(frontWheel1.rpm * 6 * Time.deltaTime,0, 0 );      

    wheelRR.Rotate(frontWheel1.rpm * 6 * Time.deltaTime,0, 0 );      

      

    var hit : WheelHit;   

         //if the wheel touches the ground, adjust graphical wheel position to reflect springs   

    if(frontWheel1.GetGroundHit(hit))   

         wheelFL.localPosition.y-=Vector3.Dot(wheelFL.position-hit.point,transform.up)-frontWheel1.radius*0.14-0.15;   

    if(frontWheel2.GetGroundHit(hit))   

         wheelFR.localPosition.y-=Vector3.Dot(wheelFR.position-hit.point,transform.up)-frontWheel2.radius*0.14-0.15;   

    if(rearWheel1.GetGroundHit(hit))   

         wheelRL.localPosition.y-=Vector3.Dot(wheelRL.position-hit.point,transform.up)-rearWheel1.radius*0.14-0.15;   

    if(rearWheel2.GetGroundHit(hit)) {   

        wheelRR.localPosition.y-=Vector3.Dot(wheelRR.position-hit.point,transform.up)-rearWheel2.radius*0.14-0.15;   

           

    }   

}     

function OnGUI()   

{   

    GUI.skin=mskin;   

    if(GUI.Button(Rect(10,10,70,30),"红"))   

        changecolor(Color.red);   

    if(GUI.Button(Rect(100,10,70,30),"白"))   

        changecolor(Color.white);   

    if(GUI.Button(Rect(200,10,70,30),"黑"))   

        changecolor(Color.black);   

    if(GUI.Button(Rect(300,10,70,30),"蓝"))   

        changecolor(Color.blue);   

    if(GUI.Button(Rect(400,10,70,30),"灰"))   

        changecolor(Color.grey);   

    if(GUI.Button(Rect(500,10,150,30),"旧电影效果"))   

    {   

        if(oldmovie==false)   

        {   

            GameObject.Find("Camera").GetComponent("ColorCorrectionEffect").enabled = true;   

            GameObject.Find("Camera").GetComponent("NoiseEffect").enabled = true;   

            oldmovie=true;   

        }   

        else  

        {   

            GameObject.Find("Camera").GetComponent("ColorCorrectionEffect").enabled = false;   

            GameObject.Find("Camera").GetComponent("NoiseEffect").enabled = false;   

            oldmovie=false;   

        }   

    }   

}




转载出处:http://blog.csdn.net/Cancer05/archive/2010/01/26/5258657.aspx
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-5 21:44 , Processed in 0.095165 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部