0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PlayerCtrl&CameraCtrl like Honkai 3rd

Last updated at Posted at 2019-07-01
using UnityEngine;

public class CameraOrbit : MonoBehaviour
{
    public Transform pivot; // the object being followed
    public Vector3 pivotOffset = Vector3.zero; // offset from target's pivot
    public float distance = 10.0f; // distance from target (used with zoom)
    public float minDistance = 2f;
    public float maxDistance = 15f;
    public float zoomSpeed = 1f;//scale speed

    public float xSpeed = 250.0f;//rotation speed
    public float ySpeed = 120.0f;
    public float damping = 0.02f;//ローテーションに対する抗力

    public bool allowYTilt = true;//允许旋转
    private float targetDistance = 0f;
    private float zoomVelocity = 1f;

    float rotateX = 0;
    float rotateY = 0;



    void Start()
    {
        transform.LookAt(pivot.position);
        var angles = transform.eulerAngles;

        Debug.Log(angles.y);

        targetDistance = distance = (transform.position - pivot.position).magnitude;
        transform.position = pivot.position - transform.forward * distance;
    }

    void LateUpdate()
    {
        if (pivot)
        {
            float scroll = Input.GetAxis("Mouse ScrollWheel");

            if (scroll > 0.0f) targetDistance -= zoomSpeed;
            else if (scroll < 0.0f) targetDistance += zoomSpeed;
            targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);

            // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
            // right mouse button must be held down to tilt/rotate cam
            // or player can use the left mouse button while holding Ctr
            if (Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))))
            {
                rotateY = Input.GetAxis("Mouse X") * xSpeed * 0.02f;

                if (allowYTilt)
                {
                    rotateX = -Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                }

            }
            if (!(Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))))
            {
                rotateX = Mathf.Lerp(rotateX, 0, damping);
                rotateY = Mathf.Lerp(rotateY, 0, damping);
            }
            transform.RotateAround(pivot.position, Vector3.up, rotateY);
            transform.RotateAround(pivot.position, transform.right, rotateX);
            distance = Mathf.Lerp(distance, targetDistance, 0.3f);
            
            Vector3 v0=transform.forward;
            Vector3 v1=pivot.position-transform.position;
            v0.y=0;//接下来要求y方向的旋转角度,所以这里y置0
            v1.y=0;
            
            Quaternion tmp=Quaternion.FromToRotation(v0,v1);//求v0到v1的旋转矩阵
            transform.rotation=tmp*transform.rotation;//进行旋转
            
            transform.position = pivot.position - transform.forward * distance;
            
        }
    }


    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);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMoveCtrl : MonoBehaviour
{
    public float Speed = 5f;
    public float JumpHeight = 2f;
    public float GroundDistance = 0.2f;
    public float DashDistance = 5f;
    public LayerMask Ground;

    private Rigidbody _body;
    private Vector3 _inputs = Vector3.zero;
    private bool _isGrounded = true;
    private Transform _groundChecker;

    public Camera camFlowPlayer;

    void Start()
    {
        _body = GetComponent<Rigidbody>();
        _groundChecker = transform.GetChild(0);
        if (camFlowPlayer == null)
        {
            camFlowPlayer = Camera.main;
        }
    }

    void Update()
    {
        _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
        

        Vector3 verX=camFlowPlayer.transform.right;
        Vector3 verZ=camFlowPlayer.transform.forward;
        verX.y=0;
        verZ.y=0;

        _inputs = Vector3.zero;
        
        _inputs += 1.3f*Input.GetAxis("Horizontal") * verX;//如果围绕摄像机旋转的话 水平方向要稍快一点
        _inputs += Input.GetAxis("Vertical")* verZ;
        if (_inputs != Vector3.zero)
            transform.forward = _inputs;
        
    }


    void FixedUpdate()
    {
        _body.MovePosition(_body.position + _inputs * Speed * Time.fixedDeltaTime);
        
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?