C# 球员相对于摄像机方向C的移动#
我对在unity中制作游戏是相当陌生的,我正在尝试让我的球员朝着相对于相机位置的方向移动。我不知道怎么做,下面是我的代码,有人能帮我吗C# 球员相对于摄像机方向C的移动#,c#,unity3d,camera,C#,Unity3d,Camera,我对在unity中制作游戏是相当陌生的,我正在尝试让我的球员朝着相对于相机位置的方向移动。我不知道怎么做,下面是我的代码,有人能帮我吗 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { // Start is called before the first frame update
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
public Rigidbody rb;
public Transform cam;
public float movementSpeed = 6.0f; //speed the player moves at
public float jumpHeight = 200.0f; //height of the jump
public float timeBetweenJumps = 1.0f; //delay between each jump so the player can't jump again before reaching the ground
private float timestamp;
public Vector3 movement;
void Start()
{
rb = this.GetComponent<Rigidbody>();
}
void Update()
{
movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
transform.LookAt(transform.position + new Vector3(movement.x, 0f, movement.z)); // makes the player always face in the direction of movement
if (Time.time >= timestamp && (Input.GetKeyDown("space")))
{
GetComponent<Rigidbody>().velocity = Vector3.up * jumpHeight; // multiplies the players Y position by the jumpHeight variable which causes the player to jump in-game
timestamp = Time.time + timeBetweenJumps;
}
}
void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector3 direction)
{
rb.MovePosition(transform.position + (direction * movementSpeed * Time.deltaTime)); //moves the rigidbody (player)
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家运动:单一行为
{
//在第一帧更新之前调用Start
公共刚体;
公共变换凸轮;
public float movementSpeed=6.0f;//播放器的移动速度
公共浮动跳跃高度=200.0f;//跳跃高度
public float TIME BETWEENHUMPS=1.0f;//每次跳跃之间的延迟,使玩家在到达地面之前不能再次跳跃
私有浮动时间戳;
公共矢量运动;
void Start()
{
rb=this.GetComponent();
}
无效更新()
{
移动=新矢量3(Input.GetAxis(“水平”),0f,Input.GetAxis(“垂直”);
transform.LookAt(transform.position+new Vector3(movement.x,0f,movement.z));//使玩家始终面向运动方向
if(Time.Time>=时间戳&&(Input.GetKeyDown(“空格”))
{
GetComponent().velocity=Vector3.up*jumpHeight;//将玩家的Y位置乘以jumpHeight变量,该变量使玩家在游戏中跳跃
timestamp=Time.Time+timeBetweenJumps;
}
}
void FixedUpdate()
{
移动字符(移动);
}
无效移动字符(矢量3方向)
{
rb.MovePosition(transform.position+(direction*movementSpeed*Time.deltaTime));//移动刚体(播放器)
}
}
首先
// multiplies the players Y position by the jumpHeight variable which causes the player to jump in-game
不,没有!你所做的是完全替换速度,所以任何前进速度都会被它覆盖。这是有意的吗?如果不是的话,它可能应该是
if (Time.time >= timestamp && (Input.GetKeyDown("space")))
{
var velocity = rb.velocity;
velocity.y = jumpHeight;
rb.velocity = velocity;
timestamp = Time.time + timeBetweenJumps;
}
那你就是在世界空间中移动
为了考虑摄像头的方向,请执行以下操作:
[SerializeField] private Camera mainCamera;
private void Awake ()
{
if(!mainCamera) mainCamera = Camera.main;
}
然后
movement = mainCamera.transform.right * Input.GetAxis("Horizontal") + mainCamera.transform.forward * Input.GetAxis("Vertical");
因此,现在使用相对于摄影机当前旋转的输入。但请注意,如果相机以某种方式在其他轴上旋转而不是仅在Y轴上旋转,这可能会表现得很奇怪