ساخت بازی اتومبیل رانی در یونیتی – اعمال متریال و تکسچر به خط ترمز

ساخت بازی اتومبیل رانی در یونیتی – اعمال متریال و تکسچر به خط ترمز

  مشخصات آموزش

  •   زبان آموزش فارسی

making car game in unity3dبا سلام خدمت دوستان عزیز

اعمال متریال و تکسچر به خط ترمز

اینم از قسمت هفدهم آموزش ساخت بازی های ماشین سواری.

توی این قسمت بیشتر روی خط ترمزی که در قسمت قبل ایجاد کردیم کار خواهیم نمود.

در این قسمت به خط ترمز یه متریال و یک تکسچر نسبت خواهیم داد.

[stextbox id=”Account_Sale”]جهت خرید این آموزش می تونید از فرم زیر استفاده نمایید.[/stextbox]

[parspalpaiddownloads id=”34″]

 

در ضمن دوستان این قسمت یه بخش اصلاح شده هم داره که به صورت رایگان قرار دادم می تونید از لینک زیر دریافتش کنید.

http://www.mediafire.com/download/d0wq2qdarms1kev/17-_SkidTexture(Modified).rar

[stextbox id=”black”]کدهای استفاده شده تا این قسمت از آموزش در زیر قابل دسترسی هستند و می توانید از این کدها استفاده نمایید.[/stextbox]

کدهای #C استفاده شده در اسکریپت CarControll:

using UnityEngine;
using System.Collections;

public class CarControll : MonoBehaviour {
    public WheelCollider WheelFL;
    public WheelCollider WheelFR;
    public WheelCollider WheelRL;
    public WheelCollider WheelRR;
    public Transform WheelFLTrans;
    public Transform WheelFRTrans;
    public Transform WheelRLTrans;
    public Transform WheelRRTrans;
    public float LowestSteerAtSpeed = 50;
    public float LowSpeedSteerAngle = 10;
    public float HighSpeedSteerAngle = 1;
    public float Deceleration = 40;
    public float TopSpeed = 150;
    public float CurrentSpeed;
    public float MaxReverseSpeed = 50;
    public float maxTorque = 50;
    public GameObject BackLightOBJ_Right;
    public GameObject BackLightOBJ_Left;
    public Material IdleBackLightMaterial;
    public Material BrakeBackLightMaterial;
    public Material ReverseBackLightMaterial;
    private bool Braked = false;
    public float MaxBrakedTorque = 100;
	private float MyForwardFriction;
	private float MySideWayFriction;
	private float SlipForwardFriction;
	private float SlipSideWayFriction;
	public int[] GearRation;
	private int i;
	// Use this for initialization
	void Start () {
        rigidbody.centerOfMass = new Vector3(0,0,0.5f);
		SetValues();
	}
	void SetValues(){
		MyForwardFriction=WheelRR.forwardFriction.stiffness;
		MySideWayFriction=WheelRR.sidewaysFriction.stiffness;
		SlipForwardFriction=0.04f;
		SlipSideWayFriction=0.08f;
	}
	// Update is called once per frame
	void FixedUpdate () {
        HandBrake();
        float SpeedFactor = rigidbody.velocity.magnitude / LowestSteerAtSpeed;
        float CurrentSteerAngle = Mathf.LerpAngle(LowSpeedSteerAngle,HighSpeedSteerAngle,SpeedFactor);
        if (Input.GetButton("Vertical") == false)
        {
            WheelRR.brakeTorque = Deceleration;
            WheelRL.brakeTorque = Deceleration;
        }
        else
        {
            WheelRR.brakeTorque =0;
            WheelRL.brakeTorque =0;
        }
        CurrentSteerAngle *= Input.GetAxis("Horizontal");
        CurrentSpeed = 2 * 3.14f* WheelRL.radius * WheelRR.rpm * 60 / 1000;
        CurrentSpeed = Mathf.Round(CurrentSpeed);
        if (CurrentSpeed < TopSpeed && CurrentSpeed>-MaxReverseSpeed && Braked==false)
        {
            WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
            WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
        }
        else
        {
            WheelRR.motorTorque =0;
            WheelRL.motorTorque =0;
        }
        WheelFL.steerAngle = CurrentSteerAngle;
        WheelFR.steerAngle = CurrentSteerAngle;
	}
    void Update()
    {
		EngineSound();
        BackLight();
        WheelPosition();
        WheelFLTrans.Rotate(WheelFL.rpm*360/60*Time.deltaTime,0,0);
        WheelFRTrans.Rotate(WheelFR.rpm * 360 / 60 * Time.deltaTime, 0, 0);
        WheelRLTrans.Rotate(WheelRL.rpm * 360 / 60 * Time.deltaTime, 0, 0);
        WheelRRTrans.Rotate(WheelRR.rpm * 360 / 60 * Time.deltaTime, 0, 0);
        WheelFLTrans.localEulerAngles = new Vector3(WheelFLTrans.localEulerAngles.x,WheelFL.steerAngle-WheelFLTrans.localEulerAngles.z,WheelFLTrans.localEulerAngles.z);
        WheelFRTrans.localEulerAngles = new Vector3(WheelFRTrans.localEulerAngles.x, WheelFR.steerAngle - WheelFRTrans.localEulerAngles.z, WheelFRTrans.localEulerAngles.z);
    }
    void BackLight()
    {
        if (CurrentSpeed > 0 && Input.GetAxis("Vertical") < 0 && Braked == false)
        {
            BackLightOBJ_Left.renderer.material = BrakeBackLightMaterial;
            BackLightOBJ_Right.renderer.material = BrakeBackLightMaterial;
        }
        else if (CurrentSpeed < 0 && Input.GetAxis("Vertical") > 0 && Braked == false)
        {
            BackLightOBJ_Left.renderer.material = BrakeBackLightMaterial;
            BackLightOBJ_Right.renderer.material = BrakeBackLightMaterial;
        }
        else if (CurrentSpeed < 0 && Input.GetAxis("Vertical") < 0 && Braked == false)
        {
            BackLightOBJ_Left.renderer.material = ReverseBackLightMaterial;
            BackLightOBJ_Right.renderer.material = ReverseBackLightMaterial;
        }
        else if(Braked==false)
        {
            BackLightOBJ_Left.renderer.material = IdleBackLightMaterial;
            BackLightOBJ_Right.renderer.material = IdleBackLightMaterial;
        }
    }
    void WheelPosition()
    {
        RaycastHit hit;
        Vector3 WheelPos;
        if (Physics.Raycast(WheelFL.transform.position, -WheelFL.transform.up, out hit, WheelFL.radius + WheelFL.suspensionDistance))
        {
            WheelPos = hit.point + WheelFL.transform.up * WheelFL.radius;
        }
        else
        {
            WheelPos = WheelFL.transform.position - WheelFL.transform.up * WheelFL.suspensionDistance;
        }
        WheelFLTrans.position = WheelPos;

        if (Physics.Raycast(WheelFR.transform.position, -WheelFR.transform.up, out hit, WheelFR.radius + WheelFR.suspensionDistance))
        {
            WheelPos = hit.point + WheelFR.transform.up * WheelFR.radius;
        }
        else
        {
            WheelPos = WheelFR.transform.position - WheelFR.transform.up * WheelFR.suspensionDistance;
        }
        WheelFRTrans.position = WheelPos;

        if (Physics.Raycast(WheelRL.transform.position, -WheelRL.transform.up, out hit, WheelRL.radius + WheelRL.suspensionDistance))
        {
            WheelPos = hit.point + WheelRL.transform.up * WheelRL.radius;
        }
        else
        {
            WheelPos = WheelRL.transform.position - WheelRL.transform.up * WheelRL.suspensionDistance;
        }
        WheelRLTrans.position = WheelPos;

        if (Physics.Raycast(WheelRR.transform.position, -WheelRR.transform.up, out hit, WheelRR.radius + WheelRR.suspensionDistance))
        {
            WheelPos = hit.point + WheelRR.transform.up * WheelRR.radius;
        }
        else
        {
            WheelPos = WheelRR.transform.position - WheelRR.transform.up * WheelRR.suspensionDistance;
        }
        WheelRRTrans.position = WheelPos;
    }
    void HandBrake()
    {
        if (Input.GetButton("Jump"))
        {
            Braked = true;
        }
        else
        {
            Braked = false;
        }

        if (Braked == true)
        {
			if(rigidbody.velocity.magnitude>1)
			{
				SetSlip(SlipForwardFriction,SlipSideWayFriction);
			}
			else
			{
				SetSlip(1,1);
			}
            WheelFR.brakeTorque = MaxBrakedTorque;
            WheelFL.brakeTorque = MaxBrakedTorque;
            WheelRR.brakeTorque = 0;
            WheelRL.brakeTorque = 0;
			if (CurrentSpeed < 1 && CurrentSpeed > -1)
        {
            BackLightOBJ_Left.renderer.material = IdleBackLightMaterial;
            BackLightOBJ_Right.renderer.material = IdleBackLightMaterial;
        }
        else 
        {
            BackLightOBJ_Left.renderer.material = BrakeBackLightMaterial;
            BackLightOBJ_Right.renderer.material = BrakeBackLightMaterial;
        }
        }
		else
		{
			SetSlip(MyForwardFriction,MySideWayFriction);
			WheelFR.brakeTorque =0;
            WheelFL.brakeTorque = 0;
		}

    }

	void SetSlip(float CurrentForwardFriction,float CurrentSidWayFriction)
	{
		WheelFrictionCurve RR_Side=WheelRR.sidewaysFriction;
		WheelFrictionCurve RL_Side=WheelRL.sidewaysFriction;
		WheelFrictionCurve FR_Side=WheelFR.sidewaysFriction;
		WheelFrictionCurve FL_Side=WheelFL.sidewaysFriction;

		WheelFrictionCurve RR_Forward=WheelRR.forwardFriction;
		WheelFrictionCurve RL_Forward=WheelRL.forwardFriction;
		WheelFrictionCurve FR_Forward=WheelFR.forwardFriction;
		WheelFrictionCurve FL_Forward=WheelFL.forwardFriction;

		RR_Forward.stiffness=CurrentForwardFriction;
		RL_Forward.stiffness=CurrentForwardFriction;
		FR_Forward.stiffness=CurrentForwardFriction;
		FL_Forward.stiffness=CurrentForwardFriction;

		RR_Side.stiffness=CurrentSidWayFriction;
		RL_Side.stiffness=CurrentSidWayFriction;
		FR_Side.stiffness=CurrentSidWayFriction;
		FL_Side.stiffness=CurrentSidWayFriction;

		WheelRR.forwardFriction=RR_Forward;
		WheelRL.forwardFriction=RL_Forward;
		//WheelFR.forwardFriction=FR_Forward;
		//WheelFL.forwardFriction=FL_Forward;

		WheelRR.sidewaysFriction=RR_Side;
		WheelRL.sidewaysFriction=RL_Side;
		//WheelFR.sidewaysFriction=FR_Side;
		//WheelFL.sidewaysFriction=FL_Side;

	}
	void EngineSound()
	{
		for(i=0;i<GearRation.Length;i++)
		{
			if(GearRation[i]>CurrentSpeed)
			{
				break;
			}
		}
		float GearMinValue=0;
		float GearMaxValue=0;
		if(i==0)
		{
			GearMinValue=0;
		}
		else
		{
			GearMinValue=GearRation[i-1];
		}
		GearMaxValue=GearRation[i];
		float EnginePitch=((CurrentSpeed-GearMinValue)/(GearMaxValue-GearMinValue))+1;
		audio.pitch=EnginePitch;
	}
}

کدهای #C استفاده شده در اسکریپت SkiddingScript:

using UnityEngine;
using System.Collections;

public class SkiddingScript : MonoBehaviour {
	private float CurrentFrictionValue;
	private float SkidAt=1.5f;
	public GameObject SkidSound;
	private float SoundEmition=10;
	private float SoundWait;
    public float MarkWidth = 0.2f;
    private int Skidding;
    private Vector3[] LastPosition = new Vector3[2];
    private int n;
    public Material SkidMaterial;
	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		WheelHit hit;
		transform.GetComponent<WheelCollider>().GetGroundHit(out hit);
		CurrentFrictionValue=Mathf.Abs(hit.sidewaysSlip);
		if(SkidAt<=CurrentFrictionValue && SoundWait<=0)
		{
			Instantiate(SkidSound,hit.point,Quaternion.identity);
			SoundWait=1;
		}
		SoundWait-=Time.deltaTime*SoundEmition;
        if (SkidAt <= CurrentFrictionValue)
        {
            SkidMesh();
        }
        else
        {
            Skidding = 0;
        }
	}
    void SkidMesh()
    {
        WheelHit hit;
        transform.GetComponent<WheelCollider>().GetGroundHit(out hit);
        GameObject mark = new GameObject("Mark");
        MeshFilter filter = mark.AddComponent<MeshFilter>();
        mark.AddComponent("MeshRenderer");
        Mesh MarkMesh = new Mesh();
        Vector3[] Vertices = new Vector3[4];
        int[] Triangles = new int[6];
        if (Skidding == 0)
        {
            Vertices[0] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(MarkWidth, 0.01f, 0);
            Vertices[1] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(-MarkWidth, 0.01f, 0);
            Vertices[2] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(-MarkWidth, 0.01f, 0);
            Vertices[3] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(MarkWidth, 0.01f, 0);
            LastPosition[0] = Vertices[2];
            LastPosition[1] = Vertices[3];
            Skidding = 1;
        }
        else
        {
            Vertices[0] = LastPosition[1];
            Vertices[1] = LastPosition[0];
            Vertices[2] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(-MarkWidth, 0.01f, 0);
            Vertices[3] = hit.point + Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z) * new Vector3(MarkWidth, 0.01f, 0);
            LastPosition[0] = Vertices[2];
            LastPosition[1] = Vertices[3];
        }
        Triangles[0] = 0;
        Triangles[1] = 1;
        Triangles[2] = 2;
        Triangles[3] = 2;
        Triangles[4] = 3;
        Triangles[5] =0;
        MarkMesh.vertices = Vertices;
        MarkMesh.triangles = Triangles;
        MarkMesh.RecalculateNormals();
        Vector2[] UVM=new Vector2[4];
        UVM[0] = new Vector2(1, 0);
        UVM[1] = new Vector2(0, 0);
        UVM[2] = new Vector2(0, 1);
        UVM[3] = new Vector2(1, 1);

        MarkMesh.uv = UVM;
        filter.mesh = MarkMesh;
        mark.renderer.material = SkidMaterial;
        mark.AddComponent<DestroySkidMaterial>();
    }

}

کدهای #C استفاده شده در اسکریپت DestroyTimerScript:

using UnityEngine;
using System.Collections;

public class DestroyTimerScript : MonoBehaviour {
	private int DestroyAfter=2;
	private float Timers;
	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		Timers+=Time.deltaTime;
		if(DestroyAfter<=Timers)
		{
			Destroy(gameObject);
		}
	}

}

کدهای #C استفاده شده در اسکریپت DestroySkidMaterial:

using UnityEngine;
using System.Collections;

public class DestroySkidMaterial : MonoBehaviour {
	private int DestroySkidMaterial_Timers=50;
	private float Timers_SkidMaterial;
	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		Timers_SkidMaterial+=Time.deltaTime;
		if(DestroySkidMaterial_Timers<=Timers_SkidMaterial)
		{
			Destroy(gameObject);
		}
	}
}

کدهای #C استفاده شده در اسکریپت CarCameraScript:

using UnityEngine;
using System.Collections;

public class CarCameraScript : MonoBehaviour {
    public Transform Car;
    public float Distance =6.4f;
    public float Height = 1.4f;
    public float RotationDamping = 1f;
    public float HeightDamping = 2;
    public float ZoomRatio = 0.5f;
    public float DefultFOV = 60;
    public Vector3 RotationVector;
    private Vector3 localVelocity;
	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void LateUpdate () {
        float wantedAngle = RotationVector.y;
        float wantedHeight = Car.position.y + Height;
        float myAngle = transform.eulerAngles.y;
        float myHeight = transform.position.y;
        myAngle = Mathf.LerpAngle(myAngle,wantedAngle,RotationDamping*Time.deltaTime);
        myHeight = Mathf.Lerp(myHeight,wantedHeight,HeightDamping*Time.deltaTime);
        Quaternion currentRotation = Quaternion.Euler(0,myAngle,0);
        transform.position = Car.position;
        transform.position -= currentRotation * Vector3.forward * Distance;
        transform.position -= currentRotation * Vector3.down * Height;
        transform.LookAt(Car);
	}

    void FixedUpdate()
    {
        localVelocity = Car.InverseTransformDirection(Car.rigidbody.velocity);
        if (localVelocity.z < -.5f)
        {
            RotationVector.y = Car.eulerAngles.y + 180;
        }
        else
        {
            RotationVector.y = Car.eulerAngles.y;
        }
        float acc = Car.rigidbody.velocity.magnitude;
        camera.fieldOfView = DefultFOV + acc * ZoomRatio;
    }
}

 

۲ Responses to “ساخت بازی اتومبیل رانی در یونیتی – اعمال متریال و تکسچر به خط ترمز”

  1. علی حیدری گفت:

    لطفا همه را رایگان کنید خاهشن ممنونم.

ارسال دیدگاه

نام (*)
پست الکترونیکی (*)
وبسایت

  مشخصات مدرس

محمودزاده بخت آباد سید حامد
دانشجوی رشته مهندسی مکانیک از دانشگاه سیستان وبلوچستان علاقه مند به مباحث گرافیکی انجام چندین طراحی شامل: طراحی تیزر - کلیپ - سایت و.... در حال حاضر مدیریت سایت Learningtv.ir رو برعهده دارم. در کل عاشق بازی سازی با یونیتی هستم.