- Home /
2D Sprite Rotation -- Help!
What aren't I declaring in the rotation part of the coding that I'm messing up with?
A friend sent me this and it comes up as "sprite" and "rotationset" do not exist in the current context, and I wasn't sure what to do.
Thanks!
using UnityEngine; using System.Collections;
public class MoveEnemy_A : MonoBehaviour {
[HideInInspector]
public GameObject[] waypoints_A;
private int currentWaypoint = 0;
private float lastWaypointSwitchTime;
public float speed = 1.0f;
void Start()
{
lastWaypointSwitchTime = Time.time;
}
void Update() {
//1
Vector3 startPosition = waypoints_A [currentWaypoint].transform.position;
Vector3 endPosition = waypoints_A [currentWaypoint + 1].transform.position;
//2
float pathLength = Vector3.Distance (startPosition, endPosition);
float totalTimeForPath = pathLength / speed;
float currentTimeOnPath = Time.time - lastWaypointSwitchTime;
gameObject.transform.position = Vector3.Lerp (startPosition, endPosition, currentTimeOnPath / totalTimeForPath);
//3
if (gameObject.transform.position.Equals (endPosition)) {
if (currentWaypoint < waypoints_A.Length - 2) {
// 3.a
currentWaypoint++;
lastWaypointSwitchTime = Time.time;
RotateIntoMoveDirection();
} else {
//3.b
Destroy(gameObject);
//TODO: deduct health
}
}
}
// Rotate enemy sprite to match direction
private void RotateIntoMoveDirection() {
// TODO: smooth out rotation
// Rotates sprite 90 degrees clockwise
sprite.transform.Rotate(0,0,-90);
}
private void SouthRotate() {
rotationset = false;
// TODO: smooth out rotation
// South - Set sprite starting rotation
sprite.transform.rotation = Quaternion.Euler(0,0,0);
// Rotation for this view has been set
rotationset = true;
}
private void EastRotate() {
rotationset = false;
// TODO: smooth out rotation
// East - Set sprite starting rotation
sprite.transform.rotation = Quaternion.Euler(0,0,90);
// Rotation for this view has been set
rotationset = true;
}
private void NorthRotate() {
rotationset = false;
// TODO: smooth out rotation
// North - Set sprite starting rotation
sprite.transform.rotation = Quaternion.Euler(0,0,180);
// Rotation for this view has been set
rotationset = true;
}
private void WestRotate() {
rotationset = false;
// TODO: smooth out rotation
// West - Set sprite starting rotation
sprite.transform.rotation = Quaternion.Euler(0,0,270);
// Rotation for this view has been set
rotationset = true;
}
}
Answer by Detour_Guide · Sep 28, 2017 at 04:24 AM
Just like it says, this script has no idea what a sprite or rotationset is. They aren't declared in this script anywhere or pulled in from other sources.
Right now this script has no idea which sprite you're trying to rotate and rotationset doesn't seem to serve any purpose within the scope of this script at this moment.
I'd suggest commenting out the rotationset lines for now and figuring out how best to tell this script which sprite you're trying to manipulate. It will depend on the rest of your project. If the sprite is on the same GameObject that this script is on, you could use GetComponent to get a reference to it.