简体   繁体   English

编辑脚本以使角色朝他所面对的方向移动

[英]Edit Script To Make Character Move In Direction He Is Facing

I am using the Third Person Controller asset by Opsive. 我正在使用Opsive的Third Person Controller资产。 It is a fairly complex third person controller that controls animations, damage, movement, inputs, etc etc. http://opsive.com/assets/ThirdPersonController/documentation.php 这是一个相当复杂的第三方控制器,用于控制动画,损坏,移动,输入等。http://opsive.com/assets/ThirdPersonController/documentation.php

I would like to update the ControllerHandler.cs script to make the character move in the direction that the character is facing, regardless of camera orientation. 我想更新ControllerHandler.cs脚本,以使角色朝着角色所面对的方向移动,而不管摄像机的方向如何。 (In the style of old Resident Evil games) (以旧版《生化危机》游戏的风格)

In the original script, the character would move forward in relation to which direction the camera was facing. 在原始脚本中,角色将相对于摄像头朝向哪个方向前进。

I received some advice to change a line in this script, but with the changes, the character moves forward in one fixed direction. 我收到了一些有关在此脚本中更改一行的建议,但是随着更改,角色朝一个固定方向前进。 (EG: When I turn to the right with the D key, and then press W to move forward, the character turns back in the direction he was originally facing and moves in that direction.) (EG:当我用D键向右转,然后按W向前移动时,角色将以他最初面对的方向转回并朝该方向移动。)

Here is the original part of the script: 这是脚本的原始部分:

#if ENABLE_MULTIPLAYER
            if ( isLocalPlayer) {
#endif
            if (m_Controller.Movement == RigidbodyCharacterController.MovementType.Combat || m_Controller.Movement == RigidbodyCharacterController.MovementType.Adventure) {
                m_LookRotation = m_CameraTransform.rotation;

Here is what someone told me to change it to: 有人告诉我将其更改为:

#if ENABLE_MULTIPLAYER
            if ( isLocalPlayer) {
#endif
            if (m_Controller.Movement == RigidbodyCharacterController.MovementType.Combat || m_Controller.Movement == RigidbodyCharacterController.MovementType.Adventure) {
                m_LookRotation = Quaternion.Euler(PlayerInput.GetAxisRaw(Constants.YawInputName), 0, 0);

Unfortunately, this does not have the result that I intended. 不幸的是,这没有我想要的结果。

Any assistance would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Here is a link to the controller script: https://docs.google.com/document/d/1B4sstqtCqRMCLuHuxEuA9I7tO_3W4aHqEZwr73uFDjY/edit?usp=sharing 这是控制器脚本的链接: https : //docs.google.com/document/d/1B4sstqtCqRMCLuHuxEuA9I7tO_3W4aHqEZwr73uFDjY/edit?usp=sharing

I think you want 我想你要

 transform.position += transform.rotation * Vector3.forward;

A complete block of code would look like this... 完整的代码块如下所示:

 void Update() {
      if (Input.GetKey(KeyCode.W)) {
           transform.position += transform.rotation * Vector3.forward * MOVESPEED;
      }
 }

I have got it working. 我已经做好了。 Take a look at the picture first it will help you set up the player correctly. 首先看一下图片,它将帮助您正确设置播放器。 My player is just two cubes. 我的玩家只有两个立方体。 I added the second cube to give a face to where the player is pointing. 我添加了第二个多维数据集,使玩家可以看到所指向的面。 I have added those 2 cube to a parent object and I move the parent object with my PlayerMovement.cs . 我已将这2个多维数据集添加到父对象,并使用PlayerMovement.cs移动了父对象。 在此处输入图片说明

public float rotSpeed;
public float playerSpeed;

    void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate( Vector3.up,  Time.deltaTime * rotSpeed);
        } else if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(-Vector3.up, Time.deltaTime * rotSpeed);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * playerSpeed);
        } else if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.Translate(-Vector3.forward * Time.deltaTime * playerSpeed);

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM