- Home /
Object faces -180 degrees away
I am building a tower defense game, but with my code, the tower faces -180 degrees away from the target. Why?
using UnityEngine;
using System.Collections;
public class Cannon : MonoBehaviour {
public GameObject projectile;
public float reloadTime = 1f;
public float rotateSpeed = 5f;
public float cooldown = .25f;
public GameObject muzzleEffect;
public float errorAmount = .001f;
public Transform target;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private float aimError;
void Update (){
if(target) {
if(Time.time >= nextMoveTime) {
CalculateAimPosition(target.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * rotateSpeed);
}
if(Time.time >= nextFireTime) {
FireProjectile();
}
}
}
void OnTriggerEnter (Collider other){
if(other.gameObject.tag == "Enemy") {
nextFireTime = Time.time + (reloadTime * .5f);
target = other.gameObject.transform;
}
}
void OnTriggerExit (Collider other){
if(other.gameObject.transform == target) {
target = null;
}
}
void CalculateAimPosition (Vector3 targetPos){
Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
void CalculateAimError (){
aimError = Random.Range(-errorAmount, errorAmount);
}
void FireProjectile (){
//audio.Play();
nextFireTime = Time.time + reloadTime;
nextMoveTime = Time.time + cooldown;
CalculateAimError();
foreach(Transform theMuzzlePos in muzzlePositions) {
Instantiate(projectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
}
Answer by robertbu · Oct 20, 2013 at 04:00 PM
Quaternion.LookRotation() takes a vector, not a position. I think you want:
Quaternion.LookRotation(aimPoint - turretBall.position);
Now it does not rotate at all. I got the following error:
transform.rotation assign attempt for 'Sphere' is not valid. Input rotation is { NaN, NaN, NaN, NaN }.
You might get this if the aimPoint and turretBall.position are the same position. You can't really look at something that is at the same position you are. Though this is must a guess since I don't have the actual code and error. But you might fix it like this:
if (aimPoint != turretBall.position) {
desiredRotation = Quaternion.LookRotation(aimPoint - turretBall.position);
}
Then we are back to the starting position. I'll leave the actual code at the current state in the bottom since you said you don't have it. I could also give you my project files if you want to take a deeper look in it. It also rotates now, but still faces the -180 degrees away.
using UnityEngine;
using System.Collections;
public class Cannon : $$anonymous$$onoBehaviour {
public GameObject projectile;
public float reloadTime = 1f;
public float rotateSpeed = 5f;
public float cooldown = .25f;
public GameObject muzzleEffect;
public float errorAmount = .001f;
public Transform target;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float next$$anonymous$$oveTime;
private Quaternion desiredRotation;
private float aimError;
void Update (){
if(target) {
if(Time.time >= next$$anonymous$$oveTime) {
CalculateAimPosition(target.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * rotateSpeed);
}
if(Time.time >= nextFireTime) {
FireProjectile();
}
}
}
void OnTriggerEnter (Collider other){
if(other.gameObject.tag == "Enemy") {
nextFireTime = Time.time + (reloadTime * .5f);
target = other.gameObject.transform;
}
}
void OnTriggerExit (Collider other){
if(other.gameObject.transform == target) {
target = null;
}
}
void CalculateAimPosition (Vector3 targetPos){
Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
if (aimPoint != turretBall.position) {
desiredRotation = Quaternion.LookRotation(aimPoint - turretBall.position);
}
}
void CalculateAimError (){
aimError = Random.Range(-errorAmount, errorAmount);
}
void FireProjectile (){
//audio.Play();
nextFireTime = Time.time + reloadTime;
next$$anonymous$$oveTime = Time.time + cooldown;
CalculateAimError();
foreach(Transform the$$anonymous$$uzzlePos in muzzlePositions) {
Instantiate(projectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
Instantiate(muzzleEffect, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation);
}
}
}
The code here looks right. Are you sure your model is constructed correctly? The "front" needs to be facing positive 'Z' when the rotation is (0,0,0). If it is totally 180 degrees off, you can "fix" the problem by reversing the parameters:
desiredRotation = Quaternion.LookRotation(turretBall.position - aimPoint);
But I'd make sure you figure out the underlying problem before making the change. If you want to give me a link to a package or a zip file of the project, I'll take a look.