JS - Own Movement Script (+ MouseLook) Smooth View

Hello @ all

First my english is not the best so please be patient :wink:

Now to my problem.
I tried to write my own movement script (walk, run, crouch, mouselook), now i have the problem that i dont know how i can set the rotation of the Player to the rotation of the camera (only the Y Rotation that the Player can turn left and right with the Mouse)

First the 2 codes:
CharController.js

#pragma strict

var walkSpeed : float = 4.0;
var runSpeedFactor : float =  2.0;
var crouchSpeedFactor : float = 2.5;
var gravity : float = 50.0;
var playerCamera : Transform;

var rotationY : float;

// Make the rigid body not change rotation
if (rigidbody)
{
	rigidbody.freezeRotation = true;
}

function Update () {

	var Controller : CharacterController = GetComponent(CharacterController);
	var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
	var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
	var height : Vector3 = transform.TransformDirection(Vector3.up);
	
	//Geschwindigkeit erhöhen wenn SHIFT Links gedrĂŒckt wird
	if(Input.GetKeyDown(KeyCode.LeftShift))
	{
		walkSpeed = walkSpeed * runSpeedFactor;
	}	
	if(Input.GetKeyUp(KeyCode.LeftShift))
	{
		walkSpeed = walkSpeed / runSpeedFactor;
	}
	
	//Geschwindigkeit verringern wenn STRG Links gedrĂŒck wird
	if(Input.GetKeyDown(KeyCode.LeftControl))
	{
		walkSpeed = walkSpeed / crouchSpeedFactor;
	}	
	if(Input.GetKeyUp(KeyCode.LeftControl))
	{
		walkSpeed = walkSpeed * crouchSpeedFactor;
	}
	
	//Character auf der X und Z Achse Bewegen
	if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
	{
		Controller.Move((vertical*(walkSpeed*Input.GetAxis("Vertical")))*Time.deltaTime);
		Controller.Move((horizontal*(walkSpeed*Input.GetAxis("Horizontal")))*Time.deltaTime);
	}
	
	//Gravitation einbinden
	Controller.Move(height*-gravity*Time.deltaTime);


	if(Input.GetButtonDown("Jump"))
	{

	}
}

function LateUpdate()
{
	//Objektrotation auf Y zu Kameraposition auf Y Àndern
	this.transform.eulerAngles.y = playerCamera.transform.eulerAngles.y;
}

MouseLook.cs (Standard from assets)

using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
///   -> A CharacterMotor and a CharacterController component will be automatically added.

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;
	public float sensitivityX = 15F;
	public float sensitivityY = 15F;

	public float minimumX = -360F;
	public float maximumX = 360F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	float rotationY = 0F;

	void Update ()
	{
		if (axes == RotationAxes.MouseXAndY)
		{
			float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
			
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
			transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
		}
		else if (axes == RotationAxes.MouseX)
		{
			transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
			
			transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}
}

In the code CharController.js the line “this.transform.eulerAngles.y = playerCamera.transform.eulerAngles.y;” works but the view is really spongy (when i move the mouse a little bit to the right or left the player turns arround like crazy).

I hope you understand what i mean und you can help me out.

Greetings:
Maxemoschen

I was unable to get your rotation line to work without an fps input controller attached to the player so I’m unsure how you’re getting the crazy turning, but if you try the following it will work with just the two scripts you posted attached. No crazy turning on my end without adding a multiplier to the line.

transform.rotation = Quaternion.Euler(0, playerCamera.transform.eulerAngles.y, 0);

Hey QuinnWinters,

i checked it and i don’t have a fps input controller attached on any player object (for better understanding i attached some images).
My chracter movement shall have the self functions (+run and crouch - without jump) like the FirstPersonController has. (i write my own becouse im to stupid to edit the “Wall of Text” from CharacterMotor.js).

You’r code do unfortunately, the same :confused:


1615335--98421--$Capsule.JPG


1615335--98424--$Hyrarchie.JPG

Try changing the Axes setting on your MouseLook script to MouseX instead of MouseXAndY, that might be the cause.

Hello Quinn Winter,

no thats not the problem (becouse i want both axis and chance the direction with the mouse and only move the player front left back and right).

If i delete these line “this.transforhttp://forum.unity3d.com/newreply.php?p=1615685&noquote=1m.eulerAngles.y = playerCamera.transform.eulerAngles.y;” the MouseMovement is ok but the player axis is wrong (if i run forward and look left with the mouse the player dont turn left too)

I added a picture whats explain what i mean