Movement with acceleration problems
Hello, I'm new to Unity and only took a C course about 4 years ago so I'm kinda new to this. I'm trying to make a character to move around using physics and a code ocurred to me:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{ // Use this for initialization
Vector3 acel, vel, v0 = Vector3.zero;
float t,x,y,z,x0,y0,z0=0, max_vel=10;
// float mass = 1;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Repositioning();
}
void Repositioning()
{
t += Time.deltaTime;
if (Input.GetKey(KeyCode.D))
{
acel=Vector3.right;
}
if (Input.GetKey (KeyCode.A))
{
acel=-Vector3.right;
}
while (vel.magnitude < max_vel)
{
vel = acel*t + v0;
v0=vel;
}
x = acel.x*Mathf.Pow(t,2) / 2 + x0;
x0 = x;
y = acel.y*Mathf.Pow(t,2) / 2 + y0;
y0 = y;
z = acel.z*Mathf.Pow(t,2) / 2 + z0;
z0 = z;
transform.Translate(x, y, z);
}
}
My Unity editor crashes after I add this Script as a component to a Sprite and try to run it to check if it works, though the compiler tells me there are no errors. Help? Pretty Please?!
Answer by D3Duck · Sep 01, 2015 at 08:54 PM
To me it looks like your while loop is infinite. What is the value of t while first running it? I'm not sure but might be 0,=. Thus your while look will never break out and you are forever stuck in frame 1.
Shouldnt that just be an if statement ins$$anonymous$$d?Did you fix the problem?