您的位置:首页 > 移动开发 > Unity3D

Unity3d基于第三人称控制简单优化

2016-05-16 17:06 459 查看
第三人称控制脚本有以下两个

角色脚本:Character1.cs

<span style="font-size:14px;">using UnityEngine;
using System.Collections;

public class Character1 : MonoBehaviour {

public float speed = 6.0F;
public float jumpSpeed = 10.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private Animator anim;
private CharacterController controller;
public Transform cameraTarget;
float Rot_y;
//private Rigidbody rigidbody;
//private int rayMask;
void Start(){
anim = GetComponent<Animator> ();
controller = GetComponent<CharacterController> ();
//rigidbody = GetComponent<Rigidbody> ();
//rayMask = LayerMask.GetMask ("Terrain");
}
void Update() {
move ();
// Turning ();
}
void move(){
if (controller.isGrounded) {
// moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));//Horizontal
// moveDirection = transform.TransformDirection(moveDirection);
// moveDirection *= speed;
if (Input.GetButtonDown ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move( moveDirection * Time.deltaTime);

if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S)) {
anim.SetInteger ("ActionID", 1);
if (Input.GetButtonDown ("Jump"))
anim.SetInteger ("ActionID", 3);
if (Input.GetKeyDown (KeyCode.W)) {
Rot_y = cameraTarget.rotation.eulerAngles.y;
} else if (Input.GetKeyDown (KeyCode.S)) {
Rot_y = cameraTarget.rotation.eulerAngles.y + 180;
} else if (Input.GetKeyDown (KeyCode.A)) {
Rot_y = cameraTarget.rotation.eulerAngles.y - 90;
} else if (Input.GetKeyDown (KeyCode.D)) {
Rot_y = cameraTarget.rotation.eulerAngles.y + 90;
}

controller.Move (transform.forward * speed * Time.deltaTime);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, Rot_y, 0), Time.deltaTime * 6);
} else if (Input.GetButtonDown ("Jump")) {
anim.SetInteger ("ActionID", 2);
}else
anim.SetInteger ("ActionID", 0);

}
// void Turning(){
// Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// RaycastHit hit;
// if (Physics.Raycast (ray, out hit, rayLength, rayMask)) {
// Vector3 dir = hit.point - transform.position;
// dir.y = 0;
// Quaternion rot = Quaternion.LookRotation (dir);
// rigidbody.MoveRotation (rot);
// }
// }
}</span><span style="font-weight: bold; color: rgb(102, 51, 0); font-size: 18px;">
</span>
摄像机脚本:Cameras.cs

<span style="font-size:14px;">using UnityEngine;
using System.Collections;

public class Cameras : MonoBehaviour {

public Transform target;
public float targetHeight = 2.0f;
public float distance = 5.0f;
public float maxDistance = 8.0f;
public float minDistance = 1.0f;
public float xSpeed = 150.0f;
public float ySpeed = 120.0f;
public int yMinLimit = -30;
public int yMaxLimit = 60;
public int zoomRate = 10;
public float zoomDampening = 10.0f;
private float x = 0.0f;
private float y = 0.0f;
private float currentDistance;
private float desiredDistance;
private float correctedDistance;
private bool grounded = false;
// private Animator anim;

void Start ()
{
// anim = target.GetComponent<Animator> ();
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
currentDistance = distance;
desiredDistance = distance;
correctedDistance = distance - 0.2f;
// if (GetComponent<Rigidbody>())
// GetComponent<Rigidbody>().freezeRotation = true;
// }
}
void LateUpdate(){
if (Input.GetMouseButton(1)){
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
//目标角色随着(左右)X轴水平旋转
target.rotation=Quaternion.Euler(new Vector3(0,x,0));//角色旋转
} else if(Input.GetMouseButton(0)){
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
//轮滚缩放摄像机视角
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
correctedDistance = desiredDistance;
Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));

RaycastHit collisionHit;
Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);
bool isCorrected = false;
if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
{
if(collisionHit.transform.name!=target.name){
position = collisionHit.point;
correctedDistance = Vector3.Distance(trueTargetPosition, position);
isCorrected = true;
}
}
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;
position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight - 0.05f, 0));
transform.rotation = rotation;
transform.position = position;
}
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);
}
}</span><strong style="font-size: 18px; color: rgb(102, 51, 0);">
</strong>

至于状态机这种技术含量的东西就不解释了



运行效果,gif要求小于2M,只能放两张漂亮的截图了。



跳跃到空中的置换效果出来了~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息