Question by
joseaguirre · Sep 09, 2015 at 05:28 AM ·
rotationvector32d-platformerlookattransform.rotate
2d Turret WORKS but is turned 90 degrees from the right way
This script here works perfectly without error but its aim is off by 90 degrees
using UnityEngine;
using System.Collections;
public class TurretRotate : MonoBehaviour {
public Transform target;
public float turretSpeed;
public float fireRate;
public float fireBallHeight;
public GameObject fireBall;
public float range;
private float _lastShotTime = float.MinValue;
float distance;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//Rotate turret to look at player.
Vector3 relativePos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
rotation.x=0;
rotation.y=0;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turretSpeed);
//Fire at player when in range.
distance = Vector3.Distance(transform.position,target.position);
if (distance < range && Time.time > _lastShotTime + (3.0f / fireRate))
{
_lastShotTime = Time.time;
launchFireBall();
}
}
void launchFireBall()
{
Vector3 position = new Vector3(transform.position.x, transform.position.y + fireBallHeight, transform.position.z);
Instantiate(fireBall, position, transform.rotation);
}
}
How do I fix this?
Comment
Answer by hexagonius · Sep 09, 2015 at 05:43 AM
lookrotation is meant to work with 3D Objects, facing along the z-Axis. making it work with 2D works like this: arrange the sprite so it points in the direction of the y-Axis. Use the two parameter version of lookrotation, with the first being Vector3.Forward and the second your aim direction.
Answer by Frizack400 · Jul 04, 2017 at 10:07 AM
Hey I have had the same problem and think I have found a way to do this quite simply. I would love your imput as its both a question and an answer. http://answers.unity3d.com/questions/1373642/using-quaternions-in-2d-to-follow-a-rotation-both.html