【发布时间】:2019-11-30 23:22:02
【问题描述】:
我正在用 C# 编写一个用于炮弹轨迹的脚本。佳能球是一个带有脚本的预制件,用于从佳能射击时的轨迹。我可以使佳能球以或多或少的正确角度射击佳能枪管倾斜的角度。但是当我尝试结合重力时,连续的佳能射击失败了。第一个佳能球似乎以正确的角度射击并遵循重力定律,但无法射出第二个佳能球。
一般来说,我们有一个佳能脚本,它可以在按下空格键时创建一个佳能球预制件。佳能脚本将枪管的旋转角度和按下空格键的时间传递给炮弹脚本。佳能脚本使用Translate方法根据角度参数平移佳能球,并通过从y-速度中减去(Time.time-createdtime)^2*gravity来结合重力机制。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//This is the script for the cannon
public class PlayerController : MonoBehaviour
{
int time = 1;
float minRotation = -90;
float maxRotation = 0;
Vector3 currentRotation;
public Transform fireBall;
canonTrajectory ct;
public float speed = 5f;
public Transform circle;
// Start is called before the first frame update
void Start()
{
ct = fireBall.GetComponent<canonTrajectory>();
currentRotation = transform.parent.localRotation.eulerAngles;
}
// Update is called once per frame
void FixedUpdate()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
currentRotation.z -= 5.0f;
currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
transform.parent.localRotation = Quaternion.Euler(currentRotation);
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
currentRotation.z += 5.0f;
currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
transform.parent.localRotation = Quaternion.Euler(currentRotation);
}
if(Input.GetKeyDown(KeyCode.Space))
{
ct.setCreatedTime(Time.time);
ct.setRotationAngle(currentRotation.z);
Transform fireInstance;
fireInstance = Instantiate(fireBall, new Vector3(transform.parent.position.x, transform.parent.position.y+3.0f, transform.position.z), transform.rotation) as Transform;
Transform c = Instantiate(circle, fireInstance.transform.position, Quaternion.identity) as Transform;
c.SetParent(fireInstance.transform);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class canonTrajectory : MonoBehaviour
{
public static float rotationAngle;
float createdTime;
float speed = 150f;
float gravity = -200.0f;
public GameObject turkey;
static int NUM_PARTICLES = 26;
Vector3[] m_position = new Vector3[NUM_PARTICLES];
LineRenderer lineRenderer;
public void setCreatedTime(float time)
{
createdTime = time;
}
public void setRotationAngle(float angle)
{
rotationAngle = angle;
}
public float GetRotationAngle()
{
return rotationAngle;
}
void Start()
{
}
void FixedUpdate()
{
float directionx = Mathf.Cos(-rotationAngle*2.0f*Mathf.PI/360.0f);
float directiony = Mathf.Sin(-rotationAngle * 2.0f * Mathf.PI / 360.0f);
Debug.Log(-rotationAngle * 2.0f * Mathf.PI / 360.0f);
float directionVectorLength = Mathf.Pow(Mathf.Pow(directionx, 2) + Mathf.Pow(directiony, 2), (0.5f));
Debug.Log("created time: "+createdTime);
transform.Translate(-5 * directionx, 2 * directiony-(Time.time-createdTime)* (Time.time - createdTime) * 2.0f , 0.0f);
if (transform.position.x < -300)
Destroy(gameObject);
if (transform.position.y < -77)
Destroy(gameObject);
}
}
这是射击动作的图片。如您所见,重力不起作用,同时发射了多个炮弹。 enter image description here
【问题讨论】:
-
创建一个空的游戏对象,作为你的枪的孩子,你想从那里射击,然后在那个位置实例化
标签: unity3d