On Trigger , stop player from moving and look at X

hello guys
I want to do exactly what is in the subject. like a guy is standing in the middle. and I put a trigger on him. I want that when main camera enter that trigger , stop scripts ( fps walker & fps look) and then camera look at a object. I used the following scripts but it doesn’t work. can you guys edit it to the one that I want? (“Durbin Asli” is my main camera")-- I just Added look at script but it doesn’t work – then I want that wait for 5 sec then everything back to normal" – I know the codes But IDK how to put them together. for example I know look at command … but I don’t know what should I do to say “main camera… look at … target” … if you guys help me and edit this for me you’d be a great help for me! :wink:

using UnityEngine;
using System.Collections;

public class Treeger : MonoBehaviour {

public Transform target;
public GameObject MC;

void Start ()
{
    MC = GameObject.Find("Durbin Asli");
}


void Update () {

}
void OnTriggerEnter()
{
    CharacterControl.tree--;
    Destroy(gameObject);
    MC.transform.LookAt(target);
}

}

Are you using the standard First Person Controller? (scripts: CharacterMotor.js, FPSInputController.js, MouseLook.cs)

1 Answer

1

It must disable the character scripts, control the camera, then reenable the scrips. Since the camera may have its own scripts, you must disable them too - the First Person Controller, for instance, uses MouseLook.cs in the camera and in the character.

A simple alternative is to use a routine that disables or enables all scripts in the character (whose collider is informed in OnTriggerEnter) and in the camera (which is informed in MC):

using UnityEngine; 
using System.Collections;
public class Treeger : MonoBehaviour {

public GameObject MC;
public Transform target;
public float timeLook = 0.5f;
public float timeWait = 4f;

void Start (){
    MC = GameObject.Find("Durbin Asli");
}

bool looking = false;

IEnumerator OnTriggerEnter (Collider obj){ // make OnTriggerEnter a coroutine
	if (looking) return; // does nothing if already looking  
	looking = true; // I'm going to look
	EnableScripts(obj.transform, false); // disable all obj scripts
	EnableScripts(MC.transform, false); // disable all camera scripts
	Quaternion rot1 = MC.transform.rotation; // save initial direction
	// define target direction in rot2
	Quaternion rot2 = Quaternion.LookRotation(target.position-MC.transform.position, Vector3.up); 
	for (float t = 0f; t < 1f;){ // look at target in timeLook seconds
		t += Time.deltaTime / timeLook;
		MC.transform.rotation = Quaternion.Lerp(rot1, rot2, t);
		yield return null;
	}
	yield return new WaitForSeconds(timeWait); // wait timeWait seconds
	for (float t = 0f; t < 1f;){ // return to original direction in timeLook seconds
		t += Time.deltaTime / timeLook;
		MC.transform.rotation = Quaternion.Lerp(rot2, rot1, t);
		yield return null;
	}
	EnableScripts(obj.transform, true); // enable obj scripts
	EnableScripts(MC.transform, true); // enable camera scripts
	looking = false; // finished looking
}

void EnableScripts (Transform owner, bool onOff){

	MonoBehaviour[] scripts;

	scripts = owner.GetComponents(); // get all scripts in owner
	foreach (MonoBehaviour script in scripts){
		script.enabled = onOff; // enable or disable all scripts
	}
}
}

Dear God! UA ate my angle brackets! It should be owner.GetComponents < MonoBehaviour >() at line 50, but this damned thing thought it was a tag! I'm trying to format this and will be back ASAP.

Problem solved: I edited my answer writing the angle brackets with that horrible &whatever; thing (&lt ; and &gt ;), but now it seems ok. I didn't find anything wrong at line 20. Try this edited script and let me know if you have any problem (I tested it before posting the answer, so except for the missing angle brackets everything was ok already).