Greetings good folk of the Unity Forum. Today my partner has requested that I post all of our problems on here in the hope that they can be browsed and fixed quickly - and to make it easier for you guys he’s made some video evidence so you can diagnose our issues more accurately! I’ll start with some contents…
- A seemingly perfect code has a crazy effect on the iPhone version of Unity
- Turret Rotation Limits
- “Jittering” on NPC’s
Here is the video, it covers the problems with some detail so you can see exactly what is happening; http://www.youtube.com/watch?v=7SsYLU9k3qs (sound is required for explaination)
Ok, let’s start with 1. -
A seemingly perfect code has a crazy effect on the iPhone version of Unity
This problem is the one that is most visible in the video - instead of aiming at the UFO and firing a single shot the NPC’s start blasting away as if they’re carrying a minigun. As my partner said, the script works properly in the editor, and it’s only after compilation that this starts to happen. Here is the script in question;
var LookAtTarget : Transform;
var damp = -1.0;
var bulletPrefab : Transform;
var thingdelay : int;
var cowxpos : float;
var cowypos : float;
function secondwait ()
{
thingdelay = 2;
yield WaitForSeconds(2);
thingdelay = 0;
}
function Update ()
{
LookAtTarget2 = transform.Find("front");
cowxpos = transform.position.x;
cowypos = transform.position.y;
if (cowxpos >= UFOcontrolBox.ufoxpos + 0.5)
{
Shoot();
if(LookAtTarget)
{
//this is where the wanted rotation is calculated
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//this spherically lerps the rotation from what it is to the wanted rotation
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
//this is the part I added, which clamps every variable between a minimum and maximum amount
rotate.eulerAngles = Vector3(Mathf.Clamp(rotate.eulerAngles.x, 180, 225), Mathf.Clamp(rotate.eulerAngles.y, 0, 0), Mathf.Clamp(rotate.eulerAngles.z, 180, 180));
}
}
if (cowxpos <= UFOcontrolBox.ufoxpos - 0.5)
{
Shoot();
if(LookAtTarget)
{
//this is where the wanted rotation is calculated
rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//this spherically lerps the rotation from what it is to the wanted rotation
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
//this is the part I added, which clamps every variable between a minimum and maximum amount
rotate.eulerAngles = Vector3(Mathf.Clamp(rotate.eulerAngles.x, 180, 225), Mathf.Clamp(rotate.eulerAngles.y, 0, 0), Mathf.Clamp(rotate.eulerAngles.z, 180, 180));
}
}
}
function Shoot()
{
if (thingdelay == 0)
{
var bullet : Object = Instantiate(bulletPrefab,transform.Find("spawnPoint").position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 350);
secondwait();
}
}
Quote: Adad
“The Editor is displaying a working code with the line “var bullet : Object = Instantiate(bulletPrefab, transform.Find(“spawnPoint”).transform.position, Quaternion.identity);” switching “Object” with “GameObject” would create an infinite spawning of bullets problem for us, the editor likes just the “Object” code, but when compiled with this code for the iPhone 4, the same problem that GameObject was giving us arises”
Problem 2. -
Turret Rotation Limits
This problem has been nagging at me for a while, and I’ve really been trying to avoid doing it for a while, but sadly this is because I’ve had no success and don’t know how to fix it! We’ve been using a variation of the standard TornadoTwins Turret Script to make our NPC’s target the UFO and fire. This has been working well, almost perfectly in fact, but my partner complains that the result looks “half assed” - by this he means the character’s arms rotate to an unrealistic angle when they shoot and he has asked that I fix it by limiting the angles at which the turret can turn to. Sadly, I don’t have a clue how to do this and help in the past has gotten nowhere (though I suspect that is due to my limited understanding of what was provided). Here is one of the targetting scripts in question;
var LookAtTarget : Transform;
var damp = -1.0;
var bulletPrefab : Transform;
var thingdelay : int;
var cowxpos : float;
var cowypos : float;
function secondwait ()
{
thingdelay = 2;
yield WaitForSeconds(2);
thingdelay = 0;
}
function Update ()
{
LookAtTarget2 = transform.Find("front");
cowxpos = transform.position.x;
cowypos = transform.position.y;
if (cowxpos >= UFOcontrolBox.ufoxpos + 0.5)
{
Shoot();
if(LookAtTarget)
{
//this is where the wanted rotation is calculated
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//this spherically lerps the rotation from what it is to the wanted rotation
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
//this is the part I added, which clamps every variable between a minimum and maximum amount
rotate.eulerAngles = Vector3(Mathf.Clamp(rotate.eulerAngles.x, 180, 225), Mathf.Clamp(rotate.eulerAngles.y, 0, 0), Mathf.Clamp(rotate.eulerAngles.z, 180, 180));
}
}
if (cowxpos <= UFOcontrolBox.ufoxpos - 0.5)
{
Shoot();
if(LookAtTarget)
{
//this is where the wanted rotation is calculated
rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//this spherically lerps the rotation from what it is to the wanted rotation
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
//this is the part I added, which clamps every variable between a minimum and maximum amount
rotate.eulerAngles = Vector3(Mathf.Clamp(rotate.eulerAngles.x, 180, 225), Mathf.Clamp(rotate.eulerAngles.y, 0, 0), Mathf.Clamp(rotate.eulerAngles.z, 180, 180));
}
}
}
function Shoot()
{
if (thingdelay == 0)
{
var bullet : Object = Instantiate(bulletPrefab,transform.Find("spawnPoint").position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 350);
secondwait();
}
}
Personally I feel that physics code is “icky”, so please provide an explaination with your solution!