Question by
Bigbossbro · Jan 16, 2017 at 10:50 AM ·
camera-movementcamera rotatecamera-lookcamera follow3rd person controller
I want to make rotation on XZ axis. But it rotates at XYZ every axis. Can anybody tell me how can I do XZ rotation?
This is a code that is attached to camera.
using UnityEngine;
using System.Collections;
public class PlayerCamera : MonoBehaviour
{
public Transform Target;
//public float Distance = 5.0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20.0f;
public float yMaxLimit = 80.0f;
private float x;
private float y;
public float CameraXOffset = 0.2f;
public float CameraYOffset = 0.0f;
public float CameraZOffset = -1.21f;
void Start()
{
}
void Awake()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
}
void LateUpdate()
{
if (Target != null)
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
float Ax;
Ax = x;
float Ay;
Ay = y ;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Quaternion AnotherRotation = Quaternion.Euler(Ay, 0, 0);
Vector3 position = rotation * (new Vector3(CameraXOffset, CameraYOffset, CameraZOffset)) + Target.position;
Vector3 AnotherPosition = AnotherRotation * (new Vector3(CameraXOffset, CameraYOffset, CameraZOffset)) + Target.position;
transform.rotation = AnotherRotation;
transform.position = AnotherPosition;
}
}
private float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
{
angle += 360;
}
if (angle > 360)
{
angle -= 360;
}
return Mathf.Clamp(angle, min, max);
}
}
And there is other code that is attached to Player.
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using System.Linq;
using System;
public class PlayerController : MonoBehaviour
{
[SerializeField] Animator Anim;
[SerializeField] Camera m_Cam;
private Vector3 moveDirection = Vector3.zero;
private bool IsGrounded = false;
private float movementSpeed = 5.0f;
public bool CanPlayerMove = true;
private float HorizontalAxis;
private float VerticalAxis;
private float DirectionDampTime = .25f;
private bool IsRunning = false;
public Transform PlayerTransform;
public Transform PlayerHeadTransform;
public float turnSpeed = 4.0f;
private Vector3 CameraOrigin;
private Vector3 PlayerOrigin;
private Vector3 VectorBetweenCameraOriginAndPlayerOrigin;
// private Vector3 offsetY;
private bool CameraCanMove = true;
private float MouseXAxis;
private float MouseYAxis;
private Vector3 MouseAxisVector;
private Vector3 VerticalAxisAndHorizontalAxis;
public float VerticalAxisAndHorizontalAxisMagnitude;
private float MouseYAxisMinLimit = -20.0f;
private float MouseYAxisMaxLimit = 80.0f;
private Vector3 CameraDistance;
private Quaternion CameraRotation;
public int Sanfs { get; private set; }
private void Start()
{
//offsetX = new Vector3(PlayerHeadTransform.position.x, PlayerHeadTransform.position.y + 8.0f, PlayerHeadTransform.position.z + 7.0f);
}
private void Update()
{
if(CanPlayerMove == true)
{
PlayerMovement();
}
}
private void LateUpdate()
{
if (CameraCanMove == true)
{
}
}
void PlayerMovement()
{
VerticalAxis = Input.GetAxis("Vertical") * 2;
HorizontalAxis = Input.GetAxis("Horizontal") * 2;
VerticalAxisAndHorizontalAxis = new Vector3(HorizontalAxis, 0f, VerticalAxis);
VerticalAxisAndHorizontalAxisMagnitude = VerticalAxisAndHorizontalAxis.magnitude;
//VerticalAxisAndHorizontalAxis = m_Cam.transform.position;
VerticalAxisAndHorizontalAxis = m_Cam.transform.TransformVector(VerticalAxisAndHorizontalAxis);//m_Cam.transform.TransformDirection(VerticalAxisAndHorizontalAxis);
Anim.SetFloat("Speed", VerticalAxisAndHorizontalAxisMagnitude);
CheckIdle();
if (VerticalAxisAndHorizontalAxisMagnitude > 0)
{
CheckWalk();
if (Input.GetKey(KeyCode.LeftShift))
{
CheckRun();
}
}
}
public void CheckRun()
{
Anim.SetBool("IsRunning", true);
Anim.SetBool("IsSprinting", false);
Anim.SetBool("IsWalking", false);
Anim.SetBool("IsIdle", false);
}
public void CheckWalk()
{
//PlayerTransform.rotation = new Quaternion(PlayerTransform.rotation.x, m_Cam.transform.rotation.y, PlayerTransform.rotation.z, PlayerTransform.rotation.w);
PlayerTransform.rotation = Quaternion.LookRotation(VerticalAxisAndHorizontalAxis.normalized);
// float PlayerRotantionAngle = m_Cam.transform.eulerAngles.y;
// PlayerTransform.position = Vector3.Scale(m_Cam.transform.TransformVector(InputMove), new Vector3(1f, 0f, 1f)).normalized;
// PlayerTransform.transform.rotation = InputMoveQuarternion;
Anim.SetBool("IsIdle", false);
Anim.SetBool("IsSprinting", false);
Anim.SetBool("IsRunning", false);
Anim.SetBool("IsWalking", true);
}
public void CheckSprint()
{
Anim.SetBool("IsIdle", false);
Anim.SetBool("IsSprinting", true);
Anim.SetBool("IsRunning", false);
Anim.SetBool("IsWalking", false);
}
public void CheckIdle()
{
//CheckButton();
Anim.SetBool("IsIdle", true);
Anim.SetBool("IsSprinting", false);
Anim.SetBool("IsRunning", false);
Anim.SetBool("IsWalking", false);
}
}
Anybody can kindly help me? I'm using unity 5.4.3f1.
Comment