Skip to content

Instantly share code, notes, and snippets.

@nastajus
Created June 15, 2014 12:57
Show Gist options
  • Save nastajus/c44c1bdf87038ae683d2 to your computer and use it in GitHub Desktop.
Save nastajus/c44c1bdf87038ae683d2 to your computer and use it in GitHub Desktop.
the snake body shakes uncontrollably when it's close to it's target it's rotating to. Not sure how to approach this problem next. vid here: http://youtu.be/rTrsRnpJ0to
using UnityEngine;
using System.Collections;
public class SnakeBody : MonoBehaviour {
private float moveSpeed = 5f;
private float rotationSpeed = 10f;
private Transform target;
private Transform source;
// Use this for initialization
void Start () {
source = transform.FindChild ("pivotFront");
target = transform.parent.GetComponent<SnakePartsFollow>().head.FindChild("pivot").transform;
Debug.Log ( source.rotation ) ;
}
// Update is called once per frame
void Update () {
// Find the difference vector pointing from the weapon to the cursor
Vector3 diff = target.position - source.position;
// Always normalize the difference vector before using Atan2 function
diff.Normalize();
// calculate the Z rotation angle using atan2
// Atan2 will give you an angle in radians, so you
// must use Rad2Deg constant to convert it to degrees
float rotZ = Mathf.Atan2(-diff.x, diff.y) * Mathf.Rad2Deg;
// now assign the roation using Euler angle function
transform.rotation = Quaternion.Euler(0f,0f,rotZ);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment