C# 将rigidbody.velocity设置为Unity2d中的鼠标方向:更新版本
是的,我知道这个问题以前被SuperSonyk问过!该帖子在这里: 然而,这篇文章的答案中提到的链接现在是遗留材料,我无法在Unity的当前版本中理解它 我的问题:简而言之,我想让我的播放器朝着光标的方向加速。我的游戏目前是一个自上而下的2D空间射击游戏。我的播放器已经使用cam.ScreenToWorldPoint正确地查看了我的鼠标。但对我来说,不懈地尝试增加力量似乎是徒劳的,因为我对编码相当陌生 如果我的代码中有任何其他问题,如果有人能指出,那就太好了! 这是我的密码:C# 将rigidbody.velocity设置为Unity2d中的鼠标方向:更新版本,c#,unity3d,C#,Unity3d,是的,我知道这个问题以前被SuperSonyk问过!该帖子在这里: 然而,这篇文章的答案中提到的链接现在是遗留材料,我无法在Unity的当前版本中理解它 我的问题:简而言之,我想让我的播放器朝着光标的方向加速。我的游戏目前是一个自上而下的2D空间射击游戏。我的播放器已经使用cam.ScreenToWorldPoint正确地查看了我的鼠标。但对我来说,不懈地尝试增加力量似乎是徒劳的,因为我对编码相当陌生 如果我的代码中有任何其他问题,如果有人能指出,那就太好了! 这是我的密码: using Sy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//================= MOVEMENT =====================
private float thrust = 0.5f;
private float maxSpeed = 10f;
public Rigidbody2D rb;
//================= AIMING =======================
public Camera cam;
Vector2 mousePos;
// Update is called once per frame: Good for INPUTS
void Update()
{
//aiming
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
// Called Set amount of times. Good for PHYSICS CALCULATIONS
void FixedUpdate()
{
Move();
speedLimiter();
//aim
Vector2 lookDirection = mousePos - rb.position;
float angle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
//======= MY MOVE FUNCTION DOES NOT WORK. HELP? ======
void Move()
{
if (Input.GetButtonDown("Accelerate"))
{
rb.velocity = new Vector3(0, thrust, 0) * Time.deltaTime;
}
}
void speedLimiter()
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
}
}
}
请清楚地总结编辑内容,谢谢 让玩家看着鼠标,你已经完成了一半的工作。我们将使用向量lookDirection作为玩家的移动方向,将其规格化,使其长度为1,并乘以推力。这样,我们就得到了一个指向鼠标方向的向量,其长度推力
void Move()
{
if (Input.GetButtonDown("Accelerate"))
{
Vector2 lookDirection = (mousePos - rb.position);
rb.AddForce(lookDirection.normalized * thrust);
}
}
请注意,我们在这里使用Vector2,因为您使用的是Rigidbody2D
另外,我认为使用Time.DeltaTime
在这里并不相关。当向物理对象应用力时,不需要使用它,根据定义,函数FixedUpdate()
具有固定的频率。如果你看看Unity的其中一个,你会发现它们不会乘以时间
最后一个提示:你可能想利用你的Rigidbody2D的属性,让你的播放器在不加速的情况下随时间减速,否则,它将失去控制,播放器将无法阻止它
如果我的答案对你来说不清楚或不完全让你满意,请不要犹豫告诉我,这是我在这里的第一个答案
编辑:我忘了告诉你,但正如derHugo提到的,因为你使用的是
Rigidbody2D
,所以你必须使用Vector2.ClampMagnitude
在限速器
功能中限制你的速度。另外,我认为你不需要你的if
,因为函数Vector2.ClampMagnitude
只会在超过最大值时改变速度值。一般来说,因为这是一个二维刚体,速度也是Vector2
,你可能更应该使用Vector2.ClampMagnitude
然后在
rb.velocity = new Vector3(0, thrust, 0) * Time.deltaTime;
您是否只想在全局Y方向移动?此外,速度已经是“每秒”值->如果重新分配新速度,则乘以Time.deltaTime
没有任何意义。因为它说的是推力,我想你更想把推力加到现有的速度上
您更愿意做的是保存您已有的鼠标方向并执行
public class PlayerController : MonoBehaviour
{
...
void FixedUpdate()
{
// I would update the aim BEFORE moving
// Use a NORMALIZED direction! Makes things easier later
Vector2 moveDirection = (mousePos - rb.position).normalized;
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
// OPTIONAL: Redirect the existing velocity into the new up direction
// without this after rotating you would still continue to move into the same global direction
rb.velocity = rb.velocity.magnitude * moveDirection;
Move(moveDirection);
speedLimiter();
}
void Move(Vector2 moveDirection)
{
if (Input.GetButtonDown("Accelerate"))
{
// Instead of re-assigning you would probably add to the existing velocity
// otherwise remove the "+" again
rb.velocity += moveDirection * thrust * Time.deltaTime;
}
}
void speedLimiter()
{
if (rb.velocity.magnitude > maxSpeed)
{
// You are working with Vector2 so use the correct method right away
rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed);
}
}
}
感谢所有回应的人! 在Brackeys社区的帮助下,我使用以下方法解决了这个问题:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//================= MOVEMENT =====================
private float thrust = 10f;
private float maxSpeed = 100f;
public Rigidbody2D rb;
//================= AIMING =======================
public Camera cam;
Vector2 mousePos;
// Update is called once per frame: Good for INPUTS
void Update()
{
//aiming
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
// Called Set amount of times. Good for PHYSICS CALCULATIONS
void FixedUpdate()
{
Move();
speedLimiter();
//aim
Vector2 lookDirection = mousePos - rb.position;
float angle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
void Move()
{
if (Input.GetKey("up"))
{
rb.AddRelativeForce(Vector2.up*thrust);
}
}
void speedLimiter()
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
}
}
//rb.AddForce(new Vector3(0, thrust, 0));
}
欢迎使用堆栈溢出。尽管您的问题已记录在案,并且问题已陈述(简而言之),但您可能希望进一步深入了解您的问题描述、您为解决问题所做的尝试以及您在程序行为中不了解的内容。有时,它可以归结为将问题分解成更小的部分……最终往往是解决自己的问题(并获得自学徽章……)。注意:当你觉得好的评论有用时,可以将其向上投票。