How to add to coding

I’m trying to create a code that, on collision of a RigidBody with a RigidBodySPSController, a sound occurs and the screen changes in some way (like a splash of red color or something along those lines to show that you’ve lost the game). I am unsure of how to incorporate new coding with what I already have.

The first part of the code is what I have and I’ve also included the coding I think I want to incorporate:

#pragma strict

public var target : Transform;
public var doFollow : boolean;

function Start () {
}

function Update () {
	if(doFollow) {
		// "interpolate" the distance from the ex to the player
		transform.position = Vector3.Lerp(transform.position, target.position, .01);
	}
}

.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
	AudioSource audio;
	
	void Start() {
		audio = GetComponent<AudioSource>();
	}
	
    void OnCollisionEnter(Collision collision) {
        foreach (ContactPoint contact in collision.contacts) {
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
        if (collision.relativeVelocity.magnitude > 2)
            audio.Play();
    }
}

If anyone knows how to help or to help me write out the code I would be forever grateful.

In the Follow script, the way Lerp is implemented will make the the object move slower as it gets closer to the target. If you want a linear movement, you can use MoveTowards instead:

#pragma strict
     
public var target : Transform;
public var doFollow : boolean;
public var speed : float = 1.0f;

function Update () {
    if(doFollow) {
        // "interpolate" the distance from the ex to the player
        //transform.position = Vector3.Lerp(transform.position, target.position, .01);
        transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    }
}

For the collision, it can be a little tricky, but there’s a chart available that explains the requirements: (http://docs.unity3d.com/Manual/CollidersOverview.html)

// Requires a collider to have isTrigger set to true
void OnTriggerEnter(Collider other) {
	if (other.GetComponent("MyFollowScript"))  // Put name of follow script
		audio.Play();
}

// Requires a rigid body to have isKinematic set to false
void OnCollisionEnter(Collision collision) {
	if (collision.collider.GetComponent("MyFollowScript"))  // Put name of follow script
		audio.Play();
}

It’s up to you if you use Trigger or Collision, the difference is: Trigger objects will pass through the target, where as RigidBody will physically push the target.

To make the screen change color, that can be done in several different ways. Using an Image Effect is probably the most ideal, but for demonstration, you can put this class on the camera:

public class BlitGraphic : MonoBehaviour {

	public Texture2D texture;
	private float remainingTime = 0f;
	public float duration = 1f;

	void Awake() {
		if (texture == null) {
			texture = new Texture2D(1, 1);
			texture.SetPixel(0, 0, new Color(1f, 0f, 0f, 0.3f));
			texture.filterMode = FilterMode.Point;
			texture.Apply();
		}
	}

	void OnGUI() {
		if (remainingTime > 0f)	{
			if (Event.current.type == EventType.Repaint) {
				Graphics.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), texture);
			}
		}
	}

	void Update() {
		remainingTime -= Time.deltaTime;
	}

	public void ShowGraphic() {
		remainingTime = duration;
	}
}

To use it, call ShowGraphic() in the Collision function:

// Requires 1 rigid body to have isKinematic set to false
void OnCollisionEnter(Collision collision) {
	if (collision.collider.GetComponent("MyFollowScript")) {  // Put name of follow script
		audio.Play();			
		BlitGraphic bt = (BlitGraphic) Camera.main.GetComponent("BlitGraphic");
		if (bt != null) bt.ShowGraphic();
	}
}