![](/img/trans.png)
[英]Trying to create a 3rd person controller in Unity but the player is not moving
[英]Unity Game Engine: 3rd Person Player Controller Script Using Rewired
我希望 MyCharacter.cs 脚本将我的 3D 角色向前、向后、向左和向右移动。 我正在使用 Rewired,这是 Unity 的高级输入系统。
它有效,但我的角色前后移动,它上下移动它们。 我仍在了解如何访问与代码相关的 y、x 和 z 轴。 我以为:
moveVector.x = player.GetAxis("Horizontal"); // get input by name or action id
moveVector.y = player.GetAxis("Vertical");
将允许我向前和向后,而不是上下 go。
这是代码:
// MyCharacter.cs - how to get input from Rewired.Player
using UnityEngine;
using System.Collections;
using Rewired;
//using AC;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
// The Rewired player id of this character
public int playerId = 0;
// The movement speed of this character
public float moveSpeed = 3.0f;
public float jumpForce;
public float impactVelocity;
// The bullet speed
public float bulletSpeed = 15.0f;
// Assign a prefab to this in the inspector.
// The prefab must have a Rigidbody componet on it in order to work.
public GameObject bulletPrefab;
private Player player; // The Rewired Player
private CharacterController cc;
private Vector3 moveVector;
private bool fire;
private void Awake()
{
//Get the Rewired Player object for this player and keep it for the duration of the character's lifetime
player = ReInput.players.GetPlayer(playerId);
// Get the character controller
cc = GetComponent<CharacterController>();
}
void Update()
{
GetInput();
ProcessInput();
}
private void GetInput()
{
// Get the input from the Rewired Player. All controllers that the Player owns will contribute, so it doesn't matter
// whetther the input is coming from a joystick, the keyboard, mouse, or a custom controller.
moveVector.x = player.GetAxis("Horizontal"); // get input by name or action id
moveVector.y = player.GetAxis("Vertical");
fire = player.GetButtonDown("Fire");
}
private void ProcessInput()
{
// Process movement
if(moveVector.x != 0.0f || moveVector.y != 0.0f)
{
cc.Move(moveVector * moveSpeed * Time.deltaTime);
}
// Process fire
if (fire)
{
GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position + transform.right, transform.rotation);
bullet.GetComponent<Rigidbody>().AddForce(transform.right * bulletSpeed, ForceMode.VelocityChange);
}
// Process jump
if(player.GetButtonDown("Jump"))
{
rb.AddForce(Vector3.up * jumpForce);
}
}
}
使用 Z 坐标代替 Y
moveVector.x = player.GetAxis("Horizontal");
moveVector.z = player.GetAxis("Vertical"); // here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.