Skip to main content

Use of Raycast to determine certain game objects ?

Raycast can be use in many fire like : There are many raycast type but these two are most that i used. 1.Physics.Raycast               Player shooting/Fire 2.Physics.SphereCast         Detect A object to Apply force due to explosion effect. Simple Physics Raycast as shown below :    RaycastHit hit;   Ray ray;    void Update ()     {         if ( Input .GetMouseButton(0))         {             ray = Camera .main.ScreenPointToRay( Input. mousePosition);               if ( Physics .Raycast(ray, out hit))             {                 Debug .Log( "Ray cast hit too " + hit.collider.gameObject.name);             }         }  }

Camera Shake in Unity 3D

Camera shake Normally used when a player get hit or you want to do something related to shaking.


// Transform of the camera to shake. Grabs the gameObject's transform

        public Transform camTransform;

// How long the object should shake for.

public float shakeDuration = 0f;

// Amplitude of the shake. A larger value shakes the camera harder.

public float shakeAmount = 0.7f;

public float decreaseFactor = 1.0f;

Vector3 originalPos;

void Awake()

        {

if (camTransform == null)

{

camTransform = GetComponent(typeof(Transform)) as Transform;

}

}

void OnEnable()

{

originalPos = camTransform.localPosition;

}

void Update()

{

if (shakeDuration > 0)

{

camTransform.localPosition = originalPos + Random.insideUnitSphere *                             shakeAmount;

shakeDuration -= Time.deltaTime * decreaseFactor;

}

else

{

shakeDuration = 0f;

                        this.enabled = false;

camTransform.localPosition = originalPos;

}

}

Comments