【发布时间】:2021-04-04 20:05:34
【问题描述】:
我是 Unity 新手,我希望玩家朝着摄像机的方向移动,但我找不到任何有效的答案。
我的移动代码在这里。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
private float speed = 50000;
private Rigidbody rb = null;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float xSpeed = Input.GetAxis("Horizontal") * speed;
float ySpeed = Input.GetAxis("Vertical") * speed;
Rigidbody rb = GetComponent<Rigidbody>();
rb.AddTorque(new Vector3(xSpeed, 0, ySpeed) * speed * Time.deltaTime);
}
}
我的相机代码在这里。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameera : MonoBehaviour
{
public float mouseSens = 2f;
float pitch;
float yaw;
public Transform target;
public float dst = 2f;
public Vector2 pitchMinMax = new Vector2(-80, 80);
// Update is called once per frame
void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * mouseSens;
pitch -= Input.GetAxis("Mouse Y") * mouseSens;
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
Vector3 targetRotation = new Vector3(pitch, yaw);
transform.eulerAngles = targetRotation;
transform.position = target.position - transform.forward * dst;
}
}
请帮助我,我是 Unity3D 的新手。
【问题讨论】:
-
那么获取相机的引用并使用
camera.transform.forward向同一方向移动? -
你能举个例子吗?