请问Unity3D中,第三人称视角使用角色控制器的Move函数移动时,怎么实现由鼠标左右移动控制运动朝向?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问Unity3D中,第三人称视角使用角色控制器的Move函数移动时,怎么实现由鼠标左右移动控制运动朝向?相关的知识,希望对你有一定的参考价值。

因为Move函数是使角色做绝对运动的,所以怎么才能实现角色朝着自己坐标的z轴朝前运动呢?如果解决了问题会有追加分感谢

Vector3 movement = Vector3 .forward* walkSpeed;
movement *= Time.deltaTime;

CharacterController cc = this.GetComponent<CharacterController>();
cc.Move(movement);
Move不是绝对运动,是按方向的
参考技术A 直接给 脚本 自己看实现,可否? 参考技术B MOVE LEFT/RIGHT

unity3d 第三人称视角的人物移动以及相机控制

何谓第三人称?就像这样:

技术分享
用wasd控制人物移动,同时保持在相机的中心。用鼠标右键与滚轮控制相机的角度和距离。
先说一下人物的移动:
首先给作为主角的单位加上 Charactor Controller组件,并调整胶囊型的碰撞体刚好包裹住主角(有其是脚底,除非你想看到你的主角能遁地,或飞行)
技术分享
然后给你的人物加上控制的脚本~
using UnityEngine;
using System.Collections;

public class move_controll : MonoBehaviour {
Transform m_transform,m_camera;//人物自己以及相机的对象
CharacterController controller;//Charactor Controller组件
public float MoveSpeed = 20.0f;//移动的速度
// Use this for initialization
void Start () {
m_transform = this.transform;//尽量不要再update里获取this.transform,而是这样保存起来,这样能节约性能
m_camera = GameObject.FindGameObjectWithTag ("MainCamera").transform;//
controller=GetComponent();
}
// Update is called once per frame
void Update () {
if ((Input.GetKey (KeyCode.W)) || (Input.GetKey (KeyCode.S)) || (Input.GetKey (KeyCode.A)) || (Input.GetKey (KeyCode.D))) {
transform.GetComponent().SetFloat("speed", "run");//将人物的动画改为移动状态,这里有个问题,就是动画组件的获取也要在update里获取,请读者自行修改吧
if (Input.GetKey (KeyCode.W)) {
//根据主相机的朝向决定人物的移动方向,下同
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y, 0);
}

if (Input.GetKey (KeyCode.S)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y+180f, 0);
}

if (Input.GetKey (KeyCode.A)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y+270f, 0);
}

if (Input.GetKey (KeyCode.D)) {
controller.transform.eulerAngles = new Vector3 (0, m_camera.transform.eulerAngles.y+90f, 0);
}

controller.Move(m_transform.forward * Time.deltaTime * MoveSpeed);
}
else
//静止状态
transform.GetComponent().SetFloat("speed", "stand");
if (Input.GetKey (KeyCode.Q)) {
transform.Translate (Vector3.up * Time.deltaTime * MoveSpeed);
}
if (!controller.isGrounded) {
//模拟简单重力,每秒下降10米,当然你也可以写成抛物线
controller.Move(new Vector3(0,-10f*Time.deltaTime,0));
}
}

然后是相机控制的脚本,从别的地方抄的,应该也不难理解,绑定在场景主相机上即可:

using UnityEngine;

public class CameraOrbit : MonoBehaviour 
{
    public Transform pivot; // the object being followed
public Vector3 pivotOffset = Vector3.zero; // offset from target‘s pivot
public Transform target; // like a selected object (used with checking if objects between cam and target)

public float distance = 10.0f; // distance from target (used with zoom)
public float minDistance = 2f;
public float maxDistance = 15f;
public float zoomSpeed = 1f;

    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;

public bool allowYTilt = true;
    public float yMinLimit = 30f;
    public float yMaxLimit = 80f;

    private float x = 0.0f;
    private float y = 0.0f;

private float targetX = 0f;
private float targetY = 0f;
private float targetDistance = 0f;
private float xVelocity = 1f;
private float yVelocity = 1f;
private float zoomVelocity = 1f;

    void Start()
    {
        var angles = transform.eulerAngles;
        targetX = x = angles.x;
targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
targetDistance = 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)) ))
            {
                targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);
}
            }
x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
if (allowYTilt) y = Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f);
else y = targetY;
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// apply
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
transform.rotation = rotation;
transform.position = position;

        }
    }

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 UnityEngine;

public class CameraOrbit : MonoBehaviour 
{
    public Transform pivot; // the object being followed
public Vector3 pivotOffset = Vector3.zero; // offset from target‘s pivot
public Transform target; // like a selected object (used with checking if objects between cam and target)

public float distance = 10.0f; // distance from target (used with zoom)
public float minDistance = 2f;
public float maxDistance = 15f;
public float zoomSpeed = 1f;

    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;

public bool allowYTilt = true;
    public float yMinLimit = 30f;
    public float yMaxLimit = 80f;

    private float x = 0.0f;
    private float y = 0.0f;

private float targetX = 0f;
private float targetY = 0f;
private float targetDistance = 0f;
private float xVelocity = 1f;
private float yVelocity = 1f;
private float zoomVelocity = 1f;

    void Start()
    {
        var angles = transform.eulerAngles;
        targetX = x = angles.x;
targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
targetDistance = 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)) ))
            {
                targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);
}
            }
x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
if (allowYTilt) y = Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f);
else y = targetY;
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// apply
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;
transform.rotation = rotation;
transform.position = position;

        }
    }

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);
}
}

 

以上是关于请问Unity3D中,第三人称视角使用角色控制器的Move函数移动时,怎么实现由鼠标左右移动控制运动朝向?的主要内容,如果未能解决你的问题,请参考以下文章

Unity3d怎么控制第三人称的朝向

unity3d第三人称wasd控制的C#代码move函数的

unity第一人称控制器脚步声在哪

Unity 3D - 使用刚体的第三人称角色移动

[Unity3D] 多人游戏中镜头固定角度、随角色移动的实现方式

关于unity3D 5.0角色控制器 不可导入 请问为何