I have been playing around with a project where I want to go around looking at objects and then be able to disable the FPSWalker and MouseLook script from my camera by pushing a button. When I will walk again I push the same button to activate the FPSWalker and MouseLook script. I started out with an example from the “Scripting manual”:
function Update () {
if (Input.GetButton ("Fire1") GetComponent (FPSWalker))
Destroy (GetComponent (FPSWalker));
}
This way I manage to disable the FPSWalker script. When I try to do the same with “MouseLook” I get an error telling me that “Unknow identifier “MouseLook””. Why will this not work with MouseLook when it works with FPSWalker??
Ok then! How to I make the scripts active again?
I did get some answers on a similiar question from “wadamw” http://forum.unity3d.com/viewtopic.php?t=21681&highlight=destroy but that one was to hard for me as I am at the beginning of scripting!
Very nice! Better then the others I have seen that just made my mouse disapear and character move funky directions.
I have a question on how to manipulate the script though. Is there a way make this same script work, only not have a key press(like the enter button in this case), but instead have the FPSWalker disable when you hit a box or sphere collider that is setup as a trigger?
Currently I have the collider setup to trigger a GUI to appear on screen with a few buttons. But I want the user to walk into the collider, have the GUI appear(done already), have the camera disable(like you have here so your not starring at the ground) and possibly smoothLookAt a target to center the camera in place(in case the user is looking at the ground when they approach the object).
var icon : Texture2D;
var isMenu : boolean = true;
var myStyle : GUIStyle;
function Start()
{
myStyle = new GUIStyle();
}
function OnGUI () {
if(GUILayout.Button(icon,myStyle))
{
isMenu = !isMenu;
if(!isMenu)
{
Destroy (GetComponent(MouseLook));
Destroy (Camera.main.GetComponent(MouseLook));
Debug.Log("mouselock");
//GetComponent.Camera.main(
}
else
{
gameObject.AddComponent("MouseLook");
Debug.Log("mouse free");
}
}
}
this will destroy the “MouseLook” script after you click on the button, and click it another time it will set a new “MouseLook” script.
the only problem is that the First Person Controller does have a mouselook script working on the X Axes and de Main Camera does have a mouselook script working on the Y Axes. if i destroy the script, and add after that a new mouselook script, it will work on the XandY Axes!
I think this is what you’re trying to do. Attach this to the sphere and set the target transform in the editor to what you want to look at. Click the unlock button to re-enable the MouseLook and allow the player to move.
using UnityEngine;
using System.Collections;
public class TriggerLook : MonoBehaviour
{
public Transform target;
Transform fpsWalker;
bool locked;
void Update()
{
if( locked )
{
fpsWalker.position = new Vector3( transform.position.x, fpsWalker.position.y, transform.position.z);
}
}
void OnGUI()
{
if( GUILayout.Button( "Unlock" ) )
{
locked = false;
DisableMouseLook( fpsWalker.gameObject, false );
}
}
void OnTriggerEnter( Collider other )
{
DisableMouseLook( other.gameObject, true );
fpsWalker = other.transform;
locked = true;
}
void DisableMouseLook( GameObject obj, bool disabled )
{
Component[] comps = obj.GetComponentsInChildren( typeof(MouseLook) );
foreach( Component c in comps )
{
if( c is MouseLook )
{
MouseLook m = c as MouseLook;
m.lookDisabled = disabled;
m.transform.LookAt( target );
}
}
}
}
EDIT: oh ya, make the “lookDisabled” variable in MouseLook public or have some other way to access it.
Thanks Shaneo! This has put me on the right path.
We did make a few changes to the scirpts transforms though. So instead of having it jump the center point of the transform it now stops at the point where the user “hits” the collider.
Here’s the script with the adjustments in case anyone is looking to do the same thing.
using UnityEngine;
using System.Collections;
public class TriggerLook : MonoBehaviour
{
public Transform target;
Transform fpsWalker;
bool locked;
float tmpX;
float tmpZ;
void Update()
{
if( locked )
{
fpsWalker.position = new Vector3(tmpX, fpsWalker.position.y, tmpZ);
}
}
void OnGUI()
{
if( GUILayout.Button( "Unlock" ) )
{
locked = false;
DisableMouseLook( fpsWalker.gameObject, false );
}
}
void OnTriggerEnter( Collider other )
{
DisableMouseLook( other.gameObject, true );
fpsWalker = other.transform;
tmpX = fpsWalker.position.x;
tmpZ = fpsWalker.position.z;
locked = true;
}
void DisableMouseLook( GameObject obj, bool disabled )
{
Component[] comps = obj.GetComponentsInChildren( typeof(MouseLook) );
foreach( Component c in comps )
{
if( c is MouseLook )
{
MouseLook m = c as MouseLook;
m.lookDisabled = disabled;
m.transform.LookAt( target );
}
}
}
}
The only thing that needs work now is making it a “smooth look at” instead of jumping the look to the target…
I only spent 5 mins on it as I’m at work right now
Thought this would be a good starting place for you. If you can’t get it work, then I can help you out later with the smooth look.
Iv’e tried several different ways to try and get the camera to lookat my object smooth, but no luck.
At one point it was doing a slight tween but not at the object I was targeting. i tried taking the smoothLookAt script and incorperating it into the script as well but its not tweening to the target but instead acting like the transform.LookAt( target ) by jumping to the target.
using UnityEngine;
using System.Collections;
public class TriggerLook : MonoBehaviour
{
bool shouldDisplay = false;
public Transform target;
Transform fpsWalker;
GameObject mysteryObject;
bool locked;
float tmpX;
float tmpZ;
float damping = 10;
Quaternion myrotation;
void Update()
{
if( locked )
{
// captures the position of the FPSWalker when the look becomes locked
fpsWalker.position = new Vector3(tmpX, fpsWalker.position.y, tmpZ);
Component[] comps = mysteryObject.GetComponentsInChildren( typeof(MouseLook) );
foreach( Component c in comps )
{
if( c is MouseLook )
{
MouseLook m = c as MouseLook;
m.lookDisabled = true;
//m.transform.LookAt( target );
myrotation = Quaternion.LookRotation(target.position - m.transform.position);
m.transform.rotation = Quaternion.Slerp(m.transform.rotation, myrotation, Time.deltaTime * damping);
}
}
}
}
void OnGUI()
{
if( shouldDisplay )
{
// Make a group on the center of the screen
GUI.BeginGroup (new Rect (50, 350, 675, 100));
// Make a box so you can see where the group is on-screen.
GUI.Box (new Rect (0,0,675,100), "");
GUI.Label (new Rect (10,0,650,100), "Question");
GUI.Button (new Rect (180,40,300,20), "yes, let's begin.");
// Button to disable the locked mouse look
if( GUI.Button (new Rect (180,70,300,20), "No I would like to look around some more."))
{
locked = false;
DisableMouseLook( fpsWalker.gameObject, false );
shouldDisplay = false;
}
// End the group we started above.
GUI.EndGroup ();
}
}
void OnTriggerEnter( Collider other )
{
DisableMouseLook( other.gameObject, true );
fpsWalker = other.transform;
mysteryObject = other.gameObject;
tmpX = fpsWalker.position.x;
tmpZ = fpsWalker.position.z;
locked = true;
shouldDisplay = true;
}
void DisableMouseLook( GameObject obj, bool disabled )
{
Component[] comps = obj.GetComponentsInChildren( typeof(MouseLook) );
foreach( Component c in comps )
{
if( c is MouseLook )
{
MouseLook m = c as MouseLook;
// m.lookDisabled = disabled;
//m.transform.LookAt( target );
}
}
}
}
I’m taking it it’s an issue with the conversion of the ‘Quaternion’??