"look at target" problem

hey… I asked sth that mr. aldo nalleto answered me… it works good But I have a problem! this script will look at the target but not perfectly… it just change the rotation of Y… I don’t know what to do… if you played fallout you see that when we start chat with someone every thing will stop and camera will go on the person ( target) I exactly want that. thanks to aldo the script just work great but looking doesn’t… I want that when I placed an object in 3d world and put that as target in this script , the camera go just on that object , from any angle , any place , just go on that… if any one can edit this to the one that I sayed it would be great …( and this is the full script that mr. aldonalleto told:)

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
        CharacterControl.tree--;
        audio.Play();
        if (looking) ; // 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
        Destroy(gameObject);
    }

void EnableScripts(Transform owner, bool onOff){
  MonoBehaviour script;
  script = owner.GetComponent<MouseLook>();
  if (script) script.enabled = onOff;
  script = owner.GetComponent<FPSWalker>();
  if (script) script.enabled = onOff;
  script = MC.GetComponent<MouseLook>();
  if (script) script.enabled = onOff;
}




    }

1 Answer

1

Hi @MobinS. In my tests this script works perfectly: when the character enters the trigger, its scripts are disabled and the camera gradually turns to the target position, wait for timeWait seconds, then returns gradually to the original direction.

There are two possibilities in your case:

1- the target pivot is way out of the mesh’s center;

2- the camera script is not being disabled, and is “fighting” with your script.

I bet on the second alternative: in the First Person Controller the camera script is responsible for the up/down movement, so you will have a problem like you’ve described if it doesn’t get disabled. EnableScripts tries to disable a camera script named MouseLook - it will fail if the camera script isn’t MouseLook. Click the Main Camera object and check which script it uses.