Unity3d 单位2D,弹道预测
我正在制作一款手机游戏,使用轨迹从我的玩家那里找到下一条路径。当玩家沉默时,轨迹运行得很好,但当玩家移动时,轨迹有时会自行移动,即使稍后它会返回到正确的位置,但这非常令人不安。 以下是脚本:Unity3d 单位2D,弹道预测,unity3d,Unity3d,我正在制作一款手机游戏,使用轨迹从我的玩家那里找到下一条路径。当玩家沉默时,轨迹运行得很好,但当玩家移动时,轨迹有时会自行移动,即使稍后它会返回到正确的位置,但这非常令人不安。 以下是脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Play
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
public static PlayerMovement Instance;
Rigidbody2D rb;
Vector3 startPos;
Vector3 endPos;
[HideInInspector] public Vector3 initPos;
[HideInInspector] public Vector3 direction;
public static Vector3 anotherSpeed; // Only for trajectory
float speedMultiplier = 1.5f;
public GameObject trajectoryDot;
GameObject[] trajectoryDots;
public int numbersOfTrajectory;
float trajectoryDotsDistance = 0.001f;
public static float energy = 100f;
public Slider energyBar;
float slowDownFactor = 0.3f;
private void Start()
{
if (!Instance)
Instance = this;
rb = GetComponent<Rigidbody2D>();
trajectoryDots = new GameObject[numbersOfTrajectory];
}
void Update()
{
anotherSpeed = direction;
if (!Pause.isPaused)
{
// Get Start Position
if (Input.GetMouseButtonDown(0))
{
// Instansiate The Trajectory
for (int i = 0; i < numbersOfTrajectory; i++)
{
trajectoryDots[i] = Instantiate(trajectoryDot,gameObject.transform.position, gameObject.transform.rotation);
}
}
// Get Position When Dragging
if (Input.GetMouseButton(0))
{
EnableSlowMotion();
// Get Drag Position
endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10);
startPos = gameObject.transform.position;
// Get The Speed
direction = endPos - startPos;
direction = direction.normalized;
direction = Vector3.Lerp(transform.position, direction, 500 * Time.deltaTime);
direction = direction * 18;
// Update The Trajectory Position
for (int i = 0; i < numbersOfTrajectory; i++)
{
trajectoryDots[i].transform.position = calculatePosition(i * trajectoryDotsDistance);
}
}
// Get Position When Realeasing
if (Input.GetMouseButtonUp(0))
{
DisableSlowMotion();
// enable Gravity
rb.gravityScale = 1f;
// Move The Player
rb.velocity = new Vector2(direction.x * speedMultiplier, direction.y * speedMultiplier);
// Destroy The Trajectory When Player Release
for (int i = 0; i < numbersOfTrajectory; i++)
{
Destroy(trajectoryDots[i]);
}
}
}
CameraZoom();
ControlsChecker();
}
// Calculate The Trajectory Prediction
Vector2 calculatePosition(float elapsedTime)
{
return new Vector2(startPos.x, startPos.y) +
new Vector2(anotherSpeed.x * speedMultiplier, anotherSpeed.y * speedMultiplier) * elapsedTime +
0.5f * Physics2D.gravity * elapsedTime * elapsedTime;
}
// Check Controls Is pull or push
void ControlsChecker()
{
if (PlayerPrefs.GetInt("Controls") == 1)
{
direction = direction;
anotherSpeed = anotherSpeed;
}
else
{
direction = -direction;
anotherSpeed = -anotherSpeed;
}
}
void EnableSlowMotion()
{
Time.timeScale = slowDownFactor;
Time.fixedDeltaTime = 0.02f * Time.timeScale;
}
void DisableSlowMotion()
{
Time.timeScale = 1f;
Time.fixedDeltaTime = 0.02F;
}
}
void CameraZoom()
{
if (Input.GetMouseButton(0))
{
vcam.m_Lens.OrthographicSize += 0.1f;
if (vcam.m_Lens.OrthographicSize >= maxZoom)
{
vcam.m_Lens.OrthographicSize = maxZoom;
}
}
else if (Input.GetMouseButtonUp(0))
{
zoomIn = true;
}
if (zoomIn)
{
vcam.m_Lens.OrthographicSize -= 0.2f;
if (vcam.m_Lens.OrthographicSize <= minZoom)
{
vcam.m_Lens.OrthographicSize = minZoom;
zoomIn = false;
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
使用UnityEngine.UI;
公共类玩家运动:单一行为
{
公共静态播放器移动实例;
刚体2d rb;
Vector3 startPos;
向量3末端位置;
[hideininstecpt]公共向量3 initPos;
[HideInInstit]公共向量3方向;
公共静态矢量3其他速度;//仅适用于轨迹
浮动速度倍增器=1.5f;
公共游戏对象轨迹点;
游戏对象[]轨迹点;
公共部门国际号码;
浮动轨迹点距离=0.001f;
公共静态浮子能量=100f;
公共能源巴;
浮动慢降系数=0.3f;
私有void Start()
{
如果(!实例)
实例=此;
rb=GetComponent();
trajectoryDots=新游戏对象[numbersOfTrajectory];
}
无效更新()
{
另一个速度=方向;
如果(!Pause.isPaused)
{
//获得起始位置
if(Input.GetMouseButtonDown(0))
{
//实例化轨迹
for(int i=0;i=最大变焦)
{
vcam.m_透镜。正交尺寸=最大变焦;
}
}
else if(Input.GetMouseButtonUp(0))
{
zoomIn=true;
}
如果(缩放)
{
vcam.m_透镜。正交尺寸-=0.2f;
如果(vcam.m_Lens.Orthographic Size您是否尝试过在LateUpdate而不是Update中执行此操作?当尝试设置某些对象相对于rigidbodyHello的位置时,它通常会有所帮助,谢谢您回答我的问题,我能够克服此问题,当我看到您的答案时,我立即想起播放更新,我使用了fixedUpdate,它解决了my问题。虽然我没有尝试更新。但多亏了你,我记得使用了另一个更新,因为到目前为止,我只在所有代码中使用更新。