我怎样才能在相机方向移动玩家

问题描述 投票:0回答:2

我需要在Unity3d中沿相机视图方向移动角色,在游戏中我有一个第三人称相机 这是移动脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movePlayer : MonoBehaviour
{
    private Rigidbody player;
    public float speed = 5f;
    private Vector3 moveVector;
    void Awake()
    {
        player = GetComponent<Rigidbody>();
    }

    void Update()
    {
        moveVector.x = Input.GetAxis("Horizontal");
        moveVector.z = Input.GetAxis("Vertical");
        player.MovePosition(player.position + moveVector * speed * Time.deltaTime);
    }
}

这是相机脚本

using UnityEngine;

public class FPCameraController : MonoBehaviour
{
    [SerializeField] private float _sensivity;
    [SerializeField] private Transform _player;
    [SerializeField] private float _verticalLover;
    [SerializeField] private float _verticalUpper;
    private float _currentVerticalAngle;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        var vertical = -Input.GetAxis("Mouse Y") * _sensivity * Time.deltaTime;
        var horizontal = Input.GetAxis("Mouse X") * _sensivity * Time.deltaTime;

        _currentVerticalAngle = Mathf.Clamp(_currentVerticalAngle + vertical, _verticalUpper, _verticalLover);
        transform.localRotation = Quaternion.Euler(_currentVerticalAngle, 0, 0);

        _player.Rotate(0, horizontal, 0);
    }
}

我尝试了很多相关材料,切换了许多移动和相机脚本,但没有任何效果

unity-game-engine unityscript
2个回答
0
投票

查看此论坛条目

以下是修改 movePlayer 脚本中的 Update 方法的方法: 请仔细检查,我目前无法测试。

movePlayer.cs

void Update()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 moveDirection = new Vector3(moveHorizontal, 0, moveVertical);
    moveDirection = Camera.main.transform.TransformDirection(moveDirection);
    moveDirection.y = 0.0f;

    player.MovePosition(player.position + moveDirection * speed * Time.deltaTime);
}

0
投票

在角色脚本中

using UnityEngine;

public class CharacterController : MonoBehaviour
{
    private static CharacterController  instance; //singleton
    public static CharacterController  Instance
    {
         get
         {
            if (instance  != null)
               return instance;
          }
     }

    public float moveSpeed = 5f; // Hareket hızı
    public Vector3 MovementDirection;
 
     private void Start()
     {
          instance = this;
      }

    private void Update()
    {

        Vector3 difference = transform.position - MovementDirection;
        difference.y = 0;
        Vector3 movement = difference.normalized * moveSpeed * Time.deltaTime;

        transform.Translate(movement); or rigidBody.veloctiy(movement * "characterSpeed"); (character speed is just a value you need to write it yourself);
    }
}

在相机脚本中:

using UnityEngine;

public class RaycastFromCamera : MonoBehaviour
{
    private Camera mainCamera;
    public float raycastDistance = 100f;
    public LayerMask groundLayer; // add terrain or ground layer mask here;

    private void Start()
    {
        mainCamera = Camera.main;
    }

    private void Update()
    {
        Ray ray = mainCamera.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f));
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, raycastDistance, groundLayer))
        {

            GameObject hitObject = hit.collider.gameObject;

            
            if (hitObject != null)
            {
                CharacterController.Instance.MovementDirection = hit.point;
            }
        }
        else
        {
            
        }

角色总是朝着相机所注视的点移动......这或多或少是这样完成的,我不知道代码中是否可能有错误,但你可以将其适应你自己的项目。您可以将其视为伪代码。

© www.soinside.com 2019 - 2024. All rights reserved.