Hey guys,
I’m struggling with a little project at the moment. I’m trying to make a bullet move in a curve. I had a go at it my self but was unsuccessful and crashed the engine a few times. I decided to do a little script pupping and found a code in Java. I managed to translate into C# and then tried to use it.
I couldn’t get any results from it. Can you please help.
I knew I would have to do some modifying to make it work in a game but, This project is meant to test my programming skills so please don’t give me all the answers, just point me in the direction to get this code to work. I think I may have also translated it wrong, possibly with the yield.
When I play it the object just stays in the one position. I did make it a ridged body and replace gravity with kismetic.
Ill post the snippet both in Java and C# so you can see if its translated wrong.
Code in Java
var startPosition : Vector3 = Vector3(-10,0,0); //The starting position in world space
var endPosition : Vector3 = Vector3(10,0,0); //The ending position in world space
var bending : Vector3 = Vector3.up; //Bend factor (on all axes)
var timeToTravel : float = 10.0; //The total time it takes to move from start- to end position
function Start () {
MoveToPosition();
}
function MoveToPosition () {
var timeStamp : float = Time.time;
while (Time.time<timeStamp+timeToTravel) {
var currentPos : Vector3 = Vector3.Lerp(startPosition, endPosition, (Time.time - timeStamp)/timeToTravel);
currentPos.x += bending.x*Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp)/timeToTravel) * Mathf.PI);
currentPos.y += bending.y*Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp)/timeToTravel) * Mathf.PI);
currentPos.z += bending.z*Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp)/timeToTravel) * Mathf.PI);
transform.position = currentPos;
yield;
}
}
The code in C#
internal Vector3 startPosition = new Vector3(-10,0,0);
internal Vector3 endPosition = new Vector3(10,0,0);
internal Vector3 bending = Vector3.up;
internal float timeToTravel = 10.0f;
// Use this for initialization
void Start ()
{
MoveToPosition();
}
IEnumerator MoveToPosition()
{
float timeStamp = Time.time;
while (Time.time < timeStamp + timeToTravel)
{
Vector3 currentPos = Vector3.Lerp(startPosition, endPosition, (Time.time - timeStamp) / timeToTravel);
currentPos.x += bending.x * Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp) / timeToTravel) * Mathf.PI);
currentPos.y += bending.y * Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp) / timeToTravel) * Mathf.PI);
currentPos.z += bending.z * Mathf.Sin(Mathf.Clamp01((Time.time - timeStamp) / timeToTravel) * Mathf.PI);
transform.position = currentPos;
yield return(0);
}
}
I thought I might be having issues with the “yield”, but I’m no expert. As well as the math to do with the currentPos x,y,z.
Again please dont give me ALL the answers, just a nudge to get me going again.
Thanks in advance ![]()