Mouselook - fly/walk script

Ok first of all, I just grabbed the MouseLookPlus script, from here http://www.unifycommunity.com/wiki/index.php?title=MouseLookPlus

because I needed a JS version of the MouseLook script. But this script doesn’t work for me :frowning:

If someone could look at it, it’d be a huge help :slight_smile:

Anyway, I made this script - in case I get MouseLookPlus to work, and I want it so that when I am grounded, I can only rotate the camera in the x-axis. But when I am not (when I am flying), I want to be able to rotate in both axises.

var moveSpeed = 3.0;
var grounded : boolean = true;

function Update () {
	var x;
	
	if (Input.GetButtonDown ("Jump")) {
		grounded = false;
	}
	
	else if(Input.GetButtonDown("F")) {
		grounded = true;
	}
	
	if (grounded == false){
		floatMove();
	}
	
	else if(grounded == true){
		groundMove();
	}
}


function groundMove() {
	x = Input.GetAxis("Horizontal");
	transform.Translate(0,0,x * Time.deltaTime * moveSpeed);
	var getC = GetComponent("MouseLookPlus");
	getC.sensivityY = 0;
}


function floatMove() {
	var getC = GetComponent("MouseLookPlus");
	var mouseLookSpeed = 15;
	getC.sensivityX = mouseLookSpeed;
	getC.sensivityY = mouseLookSpeed;
	//Alright, so, now I need to turn off the rigidbody
	//and set the mouseLook so that when I move it either up or down,
	//the player object will follow.
}
@script RequireComponent(MouseLookPlus)

Oh, yeah, and if you could help me with turning the rigidbody off from within the script, that would be great too :slight_smile:

Please take a look at it and tell me what you think :slight_smile:
Thanks

Please help

You can turn the rigidbody off temporarily using its isKinematic setting:-

rigidbody.isKinematic = true; // Set to false to re-enable.

You can move an object in the direction the camera is looking by using the camera’s transform.forward vector:-

moveDirection = Camera.main.transform.forward;

Thanks for the reply :slight_smile:
I saw your tip on the rigidbody!
However, I find that it works better by increasing rigidbody.drag, because then your object can’t fly through things.

Right now I am using the MouseLook script, but it is written in C#
I still need a JS version of MouseLook though :confused: And I am having some problems with my movement too, can’t seem to solve this problem.

The script says it all :wink:

This is my new script:

var moveSpeed = 3.0;
var grounded : boolean = true;
var cursorShow : boolean = true;

function FixedUpdate () {
	if (grounded == false){
		rigidbody.drag = 100;
	}
	
	else if (grounded == true){
		rigidbody.drag = 0;
	}
}

function Update () {
	// Move
	var x = Input.GetAxis("Vertical");
	var z = Input.GetAxis("Horizontal");
	transform.Translate(z,0,x * Time.deltaTime * moveSpeed); // This doesn't work for some reason
	// When I try to move to the side, it doesn't imply deltaTime, nor the moveSpeed variable

	
	// Call a block of code, just some mouse configuring
	cursorActivation();
	
	// Start the flying process
	if (Input.GetButtonDown ("Jump")) {
		grounded = false;
	}
	
	// End the flying process
	else if(Input.GetButtonDown("F")) {
		grounded = true;
	}
	
	// Activate a certain block of code, which is part of flying
	if (grounded == false){
		floatMove();
	}
	
	// Go back to normal
	else if(grounded == true){
		groundMove();
	}
}

function cursorActivation(){
	if (! Input.GetMouseButton(1)){
		cursorShow = true;
	}
		
	else if(Input.GetMouseButton(1)){
		cursorShow = false;
	}
	
	if(cursorShow == true){
		Screen.showCursor = true;
		gameObject.GetComponent(MouseLook).enabled = false;
	}
	
	else if(cursorShow == false){
		Screen.showCursor = false;
		gameObject.GetComponent(MouseLook).enabled = true;
	}
}

function groundMove() {
	// So far this does nothing
}


function floatMove() {
	if(Input.GetButton("Jump")){
		transform.position += Vector3.up * Time.deltaTime * moveSpeed;
	}
	
	// Okay this is the point where I need a JavaScript version of MouseLook.
	// I need to be able to set whether you're able to look up or down with the mouse and so on
}

Hope you wanna help me further!

it may be a little bit ugly, but maybe you could have a function reset the axis’ rotation back to “flat” every frame? or if its not to much trouble to your game you could have the camera fix its position in third person relative to the controller, like the tornadotwins tutorial “worminator” game? ill see if i have the code - its in javascript by the way.

function LateUpdate()
{
	if (dead)
	{
		transform.position = Vector3(0.1, 3.5, -13);
		gameObject.Find("Main Camera").transform.position = Vector3(0.1, 3.5, -23);
		dead = false;
	}
}

thats for when the character dies, but you could modify a few things - have some variables to store your player’s current x y z positions, then on command set those positions + a bit extra so theres some distance between the camera and the player.

i hope this has helped! love from ><>FreakFish<><

Thanks for answering,
I might not have expressed myself clearly enough, I’m sorry about that.

It is not even that complicated, all I want is be able to modify the MouseLook script, so that I can change the sensivityX- and Y.
This makes me able to define whether I want to change the object’s rotation in the X or Y axis, as the camera, and when I want to do it :slight_smile: This is very essential in my game, as I would be able to switch between walking and flying, just by clicking a button.

Hope it’s not too much trouble
Thanks!