需要一种方法让玩家朝着相机指向的方向移动。 (统一)
Need a way for player to move towards the direction the camera is pointing. (Unity)
我是一名初级程序员,通过编写一些第三人称移动脚本开始练习。对于相机,我使用了 Cinemachine 的 Free Look 相机,但我遇到了问题。玩家有一个设定的前进方向,但我希望这个方向可以改变到玩家面对的任何地方。我查找了一些不同的东西,但其中 none 个在我的代码中有效。任何帮助,将不胜感激。 C#。 (抱歉,并非所有代码都会进入。)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMovement : MonoBehaviour
{
public float speed = 10f;
public Rigidbody rb;
public bool BeanOnTheGround = true;
private void start() {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float vertical = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(horizontal, 0, vertical);
if(Input.GetButtonDown("Jump") && BeanOnTheGround) {
rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
BeanOnTheGround = false;
}
}
private void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "Ground") {
BeanOnTheGround = true;
}
}
}
我会尝试:
transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Camera.main.transform.up);
对于要设置旋转的游戏对象的变换。
例如,附加此脚本的游戏对象将在Start
时面向主摄像机前方。
using UnityEngine;
public class LookToCamForward : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Camera.main.transform.up);
}
}
我是一名初级程序员,通过编写一些第三人称移动脚本开始练习。对于相机,我使用了 Cinemachine 的 Free Look 相机,但我遇到了问题。玩家有一个设定的前进方向,但我希望这个方向可以改变到玩家面对的任何地方。我查找了一些不同的东西,但其中 none 个在我的代码中有效。任何帮助,将不胜感激。 C#。 (抱歉,并非所有代码都会进入。)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMovement : MonoBehaviour
{
public float speed = 10f;
public Rigidbody rb;
public bool BeanOnTheGround = true;
private void start() {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float vertical = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(horizontal, 0, vertical);
if(Input.GetButtonDown("Jump") && BeanOnTheGround) {
rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
BeanOnTheGround = false;
}
}
private void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "Ground") {
BeanOnTheGround = true;
}
}
}
我会尝试:
transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Camera.main.transform.up);
对于要设置旋转的游戏对象的变换。
例如,附加此脚本的游戏对象将在Start
时面向主摄像机前方。
using UnityEngine;
public class LookToCamForward : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward, Camera.main.transform.up);
}
}