线渲染器和子弹的射击方向不一样
Shooting direction isnt the same between line renderer and bullet
我正在实现我的第一个第三人称游戏。在这个游戏中,我想朝鼠标的方向射击。因此,我从相机向地面投射光线。为了让射击效果更好,我从玩家到当前鼠标位置画了一条线(见截图)。
如果玩家按下开火按钮,应该会产生一颗子弹并朝线条渲染器的方向射击。
不幸的是,子弹没有飞向鼠标的方向,我不确定为什么?
两个脚本:线渲染器和子弹生成和射击都在同一个游戏对象上。
当前代码
线条渲染器:
void Update()
{
Vector3 pos = new Vector3(200, 200, 0);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
linePointing = new Vector3(hit.point.x, hit.point.y, hit.point.z); // really unnecessary but I want the line Pointing as my hit point
linePointing.y = gameObject.transform.position.y;
Vector3 localCord = gameObject.transform.InverseTransformPoint(linePointing);
float angle = Mathf.Atan(localCord.x / localCord.z) * Mathf.Rad2Deg;
//This is just so the player cant shoot in every direction
if (Mathf.Abs(angle) < 80 && localCord.z > 0)
{
m_lineRenderer.SetPosition(1, localCord);
m_lineRenderer.SetPosition(2, localCord * 100);
}
}
}
子弹生成(也在更新中)
GameObject bullet = Instantiate(bulletPrefab);
bullet.transform.parent = gameObject.transform.parent;
Physics.IgnoreCollision(bullet.GetComponent<Collider>(), bulletSpawn.parent.GetComponent<Collider>());
bullet.transform.position = bulletSpawn.position;
Vector3 rotation = bullet.transform.rotation.eulerAngles;
bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
bullet.GetComponent<Rigidbody>().AddForce(getShootingDirection().normalized * bulletSpeed, ForceMode.Impulse);
//Change parent, so the player movement doesnt affect the bullet
bullet.transform.parent = null;
获取射击方向-方法
此方法与线渲染器相同,但我想将其分开
private Vector3 getShootingDirection()
{
Vector3 linePointing = Vector3.zero;
Vector3 localCord = Vector3.zero;
Vector3 pos = new Vector3(200, 200, 0);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
linePointing = new Vector3(hit.point.x, hit.point.y, hit.point.z)
{
y = hit.point.y + 0.5f
};
localCord = gameObject.transform.InverseTransformPoint(linePointing);
}
return localCord;
}
线条渲染器图片:
RigidbodyAddforce 在世界坐标中添加了一个力,我认为你是在局部坐标中添加了一个力。你可以看看。
我正在实现我的第一个第三人称游戏。在这个游戏中,我想朝鼠标的方向射击。因此,我从相机向地面投射光线。为了让射击效果更好,我从玩家到当前鼠标位置画了一条线(见截图)。
如果玩家按下开火按钮,应该会产生一颗子弹并朝线条渲染器的方向射击。
不幸的是,子弹没有飞向鼠标的方向,我不确定为什么?
两个脚本:线渲染器和子弹生成和射击都在同一个游戏对象上。
当前代码
线条渲染器:
void Update()
{
Vector3 pos = new Vector3(200, 200, 0);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
linePointing = new Vector3(hit.point.x, hit.point.y, hit.point.z); // really unnecessary but I want the line Pointing as my hit point
linePointing.y = gameObject.transform.position.y;
Vector3 localCord = gameObject.transform.InverseTransformPoint(linePointing);
float angle = Mathf.Atan(localCord.x / localCord.z) * Mathf.Rad2Deg;
//This is just so the player cant shoot in every direction
if (Mathf.Abs(angle) < 80 && localCord.z > 0)
{
m_lineRenderer.SetPosition(1, localCord);
m_lineRenderer.SetPosition(2, localCord * 100);
}
}
}
子弹生成(也在更新中)
GameObject bullet = Instantiate(bulletPrefab);
bullet.transform.parent = gameObject.transform.parent;
Physics.IgnoreCollision(bullet.GetComponent<Collider>(), bulletSpawn.parent.GetComponent<Collider>());
bullet.transform.position = bulletSpawn.position;
Vector3 rotation = bullet.transform.rotation.eulerAngles;
bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
bullet.GetComponent<Rigidbody>().AddForce(getShootingDirection().normalized * bulletSpeed, ForceMode.Impulse);
//Change parent, so the player movement doesnt affect the bullet
bullet.transform.parent = null;
获取射击方向-方法 此方法与线渲染器相同,但我想将其分开
private Vector3 getShootingDirection()
{
Vector3 linePointing = Vector3.zero;
Vector3 localCord = Vector3.zero;
Vector3 pos = new Vector3(200, 200, 0);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
linePointing = new Vector3(hit.point.x, hit.point.y, hit.point.z)
{
y = hit.point.y + 0.5f
};
localCord = gameObject.transform.InverseTransformPoint(linePointing);
}
return localCord;
}
线条渲染器图片:
RigidbodyAddforce 在世界坐标中添加了一个力,我认为你是在局部坐标中添加了一个力。你可以看看。