Hey,
i am currently working on a 3D snake game.
The code looks like this:
public class SnakeScript : MonoBehaviour
{
//movement
private Rigidbody rb;
public float force;
public float turnspeed;
//spawning
public int spawndelay;
private GameObject activesegment;
private GameObject activetail;
private List<Vector3> lastpos = new List<Vector3>();
private List<Quaternion> lastrotation = new List<Quaternion>();
public List<GameObject> segments = new List<GameObject>();
public float spawnthreshold;
private bool spawninque = false;
//graphic
public int skin;
public List<GameObject> heads = new List<GameObject>();
public List<GameObject> bodys = new List<GameObject>();
public List<GameObject> tails = new List<GameObject>();
private bool hastail = false;
private GameObject tail;
private int i = 1;
private void Start()
{
activetail = tails[skin];
activesegment = bodys[skin];
rb = GetComponent<Rigidbody>();
GameObject body = Instantiate(heads[skin], transform.position, transform.rotation);
body.transform.SetParent(this.gameObject.transform);
//InvokeRepeating("addnewSegment", 3.0f, 2f);
}
private void Update()
{
if (spawninque)
{
addnewSegment();
spawninque = false;
}
Move();
lastpos.Insert(0, transform.position);
lastrotation.Insert(0, transform.rotation);
UpdateSegments();
int index = (segments.Count+2) * spawndelay;
if(index < lastpos.Count){
lastpos.RemoveRange(index, lastpos.Count - index);
}
}
public void Move()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.position.x > Screen.width / 2)
{
print("Moveleft");
}
else
{
print("MoveRights");
}
}
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x > Screen.width / 2)
{
//rechts
rb.AddRelativeTorque(turnspeed * transform.up * Time.deltaTime, ForceMode.Force);
}
else
{
//Link
rb.AddRelativeTorque(-turnspeed * transform.up * Time.deltaTime, ForceMode.Force);
}
}
rb.angularVelocity = Vector3.Lerp(rb.angularVelocity, Vector3.zero, 0.1f);
//rb.velocity = force * transform.forward * Time.deltaTime;
transform.position += force * transform.forward;
//rb.MovePosition(transform.position + force * transform.forward);
//rb.position += force * transform.forward;
}
private void FixedUpdate()
{
//rb.velocity = force * transform.forward;
}
public void addnewSegment()
{
if (Vector3.Distance(transform.position, lastpos[spawndelay * i]) > spawnthreshold)
{
if (hastail)
{
GameObject newsegment = Instantiate(activesegment, lastpos[spawndelay * i], lastrotation[spawndelay * i]);
segments.Add(newsegment);
newsegment.GetComponent<SegmentScript>().spawnint = spawndelay * (i - 1);
tail.GetComponent<SegmentScript>().spawnint += spawndelay;
i++;
}
else
{
tail = Instantiate(activetail, lastpos[spawndelay * i], lastrotation[spawndelay * i]);
segments.Add(tail);
tail.GetComponent<SegmentScript>().spawnint = spawndelay * i;
i++;
hastail = true;
}
}
else
{
spawninque = true;
}
}
void UpdateSegments()
{
foreach (GameObject intsegment in segments)
{
intsegment.transform.position = lastpos[intsegment.GetComponent<SegmentScript>().spawnint];
intsegment.transform.rotation = lastrotation[intsegment.GetComponent<SegmentScript>().spawnint];
}
}
}
so basically I move my object, then the position and rotation to a list and have all the snake segments follow that list.
The problem is that with using rb.velocity = force * transform.forward; I get jittery forward movement.
When using transform.position += force * transform.forward; I get jittery collisions. What am I doing wrong?
I´ve tried every combination of fixedupdate and normal with or without fixed delta time.