- Home /
Question by
nestor_d · Feb 03, 2015 at 07:46 PM ·
rotationmovementunitsreal-time-strategy
Adapting RST movement script to 3D
Hi people.
I'm trying to adapt the movement code from Brent Farris's RTS tutorial (https://www.youtube.com/watch?v=Z8BpBNntn9g) to a 3D environment. I had no trouble with changing the movement part of the code, worked perfectly in 3D. But when I tried to implement the rotation part my units began doing some crazy movements. Here is the unit rotation part of the code in the way I tried to adapt it, with my additions marked with comments. The units take my commands but instead of going where I tell them they simply spin around forever or go their own way somewhere else. Any ideas on how to fix this? Thanks!
if (!lockedAngle) {
float angle = Vector3.Angle (direction, transform.forward);
if (!choseDirection)
{
if (direction.x < 0)
currentRotationSpeed = -rotationSpeed;
else
currentRotationSpeed = rotationSpeed;
if (direction.y < 0) //I added this and the next three lines
currentRotationSpeed = -rotationSpeed;
else
currentRotationSpeed = rotationSpeed;
choseDirection = true;
}
if (angle > rotationSpeed)
transform.Rotate (new Vector3 (currentRotationSpeed, currentRotationSpeed, 0)); //changed the value of x from 0 to currentRotation speed
else {
transform.LookAt (new Vector3 (transform.position.x, transform.position.y, moveToDst.z)); //changed "moveToDst.x" to "transform.position.x"
lockedAngle = true;
Comment