角色移动控制
相机控制
using UnityEngine;
using System.Collections;
//using DG.Tweening;
using UnityEngine.EventSystems;
public class WowMainCamera : MonoBehaviour
{
private float offset = 1.3f;
public Transform target;
public float targetHeight = 0;
public float maxDistance = 100;
public float minDistance = -50;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public int yMinLimit = -90;
public int yMaxLimit = 90;
public int zoomRate = 40;
public float zoomDampening = 10.0f;
public float x;
public float y;
public float currentDistance;
public float desiredDistance;
private float correctedDistance;
public float dragSpeed = 10;
public bool allowControl = true;
public bool allowZoom = true;
public bool allowDrag = true;
public bool AgreewithViewOnEnable = false;
//private float damping = 1f;
private void Start()
{
//target = GameObject.Find("chuan").transform;
}
private void OnEnable()
{
if (AgreewithViewOnEnable)
{
AgreewithView();
}
}
private void AgreewithView()
{
bool tempControl = allowControl;
bool tempZoom = allowZoom;
allowZoom = false;
allowControl = false;
x = transform.eulerAngles.y;
y = transform.eulerAngles.x;
Vector3 pos = new Vector3(transform.position.x, transform.position.y - targetHeight, transform.position.z);
GameObject tempTarget = new GameObject();
pos += transform.forward * desiredDistance;
tempTarget.transform.position = pos;
Transform originTarget = target;
target = tempTarget.transform;
// tempTarget.transform.DOMove(originTarget.position, 1f).OnComplete(() =>
// {
// target = originTarget;
// Destroy(tempTarget);
// allowZoom = tempZoom;
// allowControl = tempControl;
// });
}
public void cameralock(bool value)
{
allowControl = value;
allowZoom = value;
allowDrag = value;
}
public void cameralock(bool Control, bool zoom, bool drag)
{
allowControl = Control;
allowZoom = zoom;
allowDrag = drag;
}
public bool OverUI;
void LateUpdate()
{
if (!OverUI)
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
}
// if (CamLimit.isOnUI)
// {
// return;
// }
if (allowControl)
{
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
}
y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
x = x % 360;
y = y % 360;
// set camera rotation
Quaternion rotation = Quaternion.Euler(y, x, 0);
if (allowDrag)
{
if (Input.GetMouseButton(2))
{
target.transform.Translate(-Input.GetAxis("Mouse X") * Time.deltaTime * dragSpeed, -Input.GetAxis("Mouse Y") * Time.deltaTime * dragSpeed, 0, this.transform);
}
}
// calculate the desired distance
if (allowZoom)
{
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * 5;
}
//gameObject.camera.orthographicSize -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate*50;
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
correctedDistance = desiredDistance;
// calculate desired camera position
Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));
//target.transform.eulerAngles = transform.eulerAngles;
// if there was a collision, correct the camera position and calculate the corrected distance
bool isCorrected = false;
// For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
// recalculate position based on the new currentDistance
position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight - 0.05f, 0));
//transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.rotation = rotation;
transform.position = position;
RaycastHit hit;
if (Physics.Linecast(new Vector3(target.position.x, target.position.y + offset, target.position.z), transform.position, out hit, ~1 << 5))
{
transform.position = hit.point;
}
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
通过CharacterController控制角色移动,移动方向与相机方向相关,同时控制角色的动画状态
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
AudioSource audio;
public AudioClip[] audioClip;
Quaternion newPos;
CharacterController controller;
public Vector3 moveDirection = Vector3.zero;
public float moveSpeed = 3;
public Animation aniMator;
float speed = 0;
public bool isRun = false;
public bool CanMove = true;
public Transform CameraTransform;
void OnEnable()
{
controller = this.transform.GetComponent<CharacterController>();
//aniMator = this.transform.GetComponent<Animation>();
// audio = this.transform.GetComponent<AudioSource>();
//CameraTransform = FrameEntrance.Instance.objCache.getGameObject("Camera/vThirdPersonCamera").transform;
// CameraTransform = GameObject.Find("Main Camera").transform;
// AudioClip clip1 = Resources.Load("Footstep01.wav") as AudioClip;
// audioClip[0]=clip1;
//CanMove = true;
moveDirection = Vector3.zero;
isRun = false;
//moveSpeed = 3;
}
void Update()
{
if (CanMove)
{
MoveControl();
}
else
{
NoMove();
}
}
void NoMove()
{
PlayAnimation("idle-New");
}
void MoveControl()
{
//AnimatorStateInfo ASI = aniMator.GetCurrentAnimatorStateInfo(0);
//if (ASI.IsName("Run_stop") || ASI.IsName("TestHigh") || ASI.IsName("celiang") || ASI.IsName("kanyiqi"))
//{
// return;
//}
//else if (ASI.IsName("cut"))
//{
// return;
//}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D))
{
Vector3 playerEuler = this.transform.rotation.eulerAngles;
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y - 45, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y + 45, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y + 135, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y - 135, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.A))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y - 90, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.D))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y + 90, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.S))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y + 180, this.transform.eulerAngles.z);
}
else if (Input.GetKey(KeyCode.W))
{
newPos = Quaternion.Euler(this.transform.eulerAngles.x, CameraTransform.eulerAngles.y, this.transform.eulerAngles.z);
if (speed == 0)
{
speed = 1;
}
}
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, newPos, 5f * Time.deltaTime);
//if (!ASI.IsName("cut"))
//{
// aniMator.SetBool("isWalk", true);
// float a = Mathf.Abs(Input.GetAxis("Horizontal"));
// float b = Mathf.Abs(Input.GetAxis("Vertical"));
// speed = Mathf.Max(a, b);
//}
if (controller.enabled)
{
moveDirection = this.transform.TransformDirection(Vector3.forward);
moveDirection *= moveSpeed;
if (isRun)
{
moveDirection *= 1.5f;
PlayAnimation("run-New");
//aniMator.SetBool("isRun", true);
}
if (!isRun)
{
moveDirection /= 1.5f;
PlayAnimation("run-New");
// aniMator.SetBool("isRun", false);
}
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime * speed);
}
}
else
{
PlayAnimation("idle-New");
}
if (!controller.isGrounded)
{
controller.Move(new Vector3(0, -1f, 0));
}
}
void PlayAnimation(string clipName) {
if (aniMator!=null)
{
aniMator.CrossFade(clipName);
}
}
}

浙公网安备 33010602011771号