Hello @ all
First my english is not the best so please be patient
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