- Home /
2D Projectile angle problem with angle
I'm having a 'quite' troubling issue with my 2D topdown shooting game.
targetPosition is the target position (obviously)
The projectile is a simple sprite; but when firing which fires in the x-y plane but the sprite is 90° rotated on the Y axis. For now Bullet is a RigidBody (3D) not 2D
What am I doing wrong here ?
UPDATE: code
/// <summary>
/// Does the actual attack.
/// </summary>
/// <param name="targetPosition">The position to attack.</param>
public void Attack(Vector3 targetPosition)
{
GameObject.Instantiate(bullet, transform.position, Quaternion.LookRotation(targetPosition - transform.position));
lastAttackTime = Time.time;
}
Bullet Script
Using UnityEngine;
public class Bullet : MonoBehaviour
{
// The speed of the bullet
public float speed;
// The amount of damage the bullet does
public float damageAmount = 5;
// Destroy itself after this amount of time
public float selfDestructTime = 5;
private Rigidbody m_Rigidbody;
private Transform m_Transform;
/// <summary>
/// Cache the component references and initialize the default values.
/// </summary>
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
m_Transform = transform;
Invoke("SelfDestruct", selfDestructTime);
}
/// <summary>
/// Move in the forward direction.
/// </summary>
void Update()
{
m_Rigidbody.MovePosition( m_Rigidbody.position + speed * m_Transform.forward * Time.deltaTime);
}
/// <summary>
/// Perform any damage to the collided object and destroy itself.
/// </summary>
/// <param name="collision"></param>
private void OnCollisionEnter(Collision collision)
{
IDamageable damageable;
if ((damageable = collision.gameObject.GetComponent(typeof(IDamageable)) as IDamageable) != null) {
damageable.Damage(damageAmount);
Destroy(gameObject);
}
}
/// <summary>
/// Destroy itself.
/// </summary>
private void SelfDestruct()
{
Destroy(gameObject);
}
}
Is the bullet sprite by default aligned with the x-axis, or with the y-axis? Your code is assu$$anonymous$$g it's with the x-axis, as you calculate the angle relative to it.
What you mean 'aligned' ? The sprite object is with rotation (0, 0, 0)
What I mean is, if you instantiate the object without any rotation (or just drag the prefab bullet into the scene), which direction is it pointing towards? It should like you fired it along the x-axis; if not, that's the reason for your 90 degree offset.
Ohh ok i see; yes it's aligned with the X-axis. But i think the main problem is with the update function which don't work in 2D; if i'm not mistaken. The bullet is being instantiated but it doesn't move toward the target.
As far as I know it should work. Is your game in 2D mode, or 3D mode but in the xy-plane only? I'm assu$$anonymous$$g m_Rigidbody etc are 2D objects…?
$$anonymous$$aybe you could rotate the source image of your sprite by 90 degrees. To counter it.
I did; the problem is not with the sprite it's with the rotation that is wrong.
Could you post a screenshot showing how the bullet looks when fired compared to how it should look? $$anonymous$$aybe I'm misunderstanding the rotation.
Just did. The original code was for 3D and i'm trying to flip 2D
Answer by CosmoM · Nov 04, 2015 at 06:53 PM
I'm not sure why the object apparently instantiates in the wrong plane, but you can fix it by changing the y-angle in your call to Instantiate to 90f (or -90f).
Can you provide an example; i don't understand your 'y-angle' thing.
Change:
GameObject.Instantiate(bullet, transform.position, Quaternion.LookRotation(targetPosition - transform.position));
to:
GameObject.Instantiate(bullet, transform.position, Quaternion.Euler(0f,90f,rotz));
where rotz is as you defined it before you edited your post and removed it. I believe you had:
Vector3 diff=targetPosition-transform.position; float rotz=$$anonymous$$athf.Atan2(diff.y,diff.x)*$$anonymous$$athf.Rad2Deg;
Ok that definitely improved the sprite rotation; but now it's the Update function that is wrong somehow transform.forward is Vector.Zero ...
I've tried to calculate Direction from the transform.rotation.z :
float angle = m_Transform.rotation.z;
Debug.Log("angle: " + angle);
direction = new Vector2((float)$$anonymous$$athf.Cos(angle), -(float)$$anonymous$$athf.Sin(angle));
m_Rigidbody.$$anonymous$$ovePosition( (Vector2) m_Rigidbody.position + speed * (Vector2) direction * Time.deltaTime);
But again messed up :(
Ok never $$anonymous$$d it was all about transform.right in 2D ... :) Thanks !