Instantiated bullet not pointing towards targeting or moving 2D
I'm making this game where at the moment you are a space ship on a side scrolling level shooting aliens. I'm trying to get the aliens to shoot back, and when the lazers are instantiated I want them to point at the player and fire. However, they just drop right in front of the alien. Here is my code: public Rigidbody2D rb; public float fireSpeed;
void Start()
{
Vector2 moveDirection = rb.velocity;
if (moveDirection != Vector2.zero)
{
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
rb.AddForce(-transform.right * fireSpeed);
}
}
}
This is my script for instantiating
Instantiate(EnemyLazerSide, new Vector3 (enemyPos.position.x - 2, enemyPos.position.y - 1, 0), Quaternion.identity);
Thank you in advance, I've only been on unity for a couple weeks, so some explanation of what you did would be nice.
Answer by highpockets · May 01, 2020 at 05:59 AM
Not sure why you answered your question with me as the answer. I’m quite flattered, thanks..
Not sure what you mean by drop right in front of the alien, but if you want to make one object look at another, you can use transform.LookAt(otherTransform)
, this is dependant on the “forward“ direction of your model (the way that your model is facing) being aligned with its blue or z axis In local space though.
If the model is not aligned with the blue/z axis, then you can either use a 3D authoring tool such as blender to adjust the model and reimport it, or you could manually rotate the object until it looks aligned and then apply a new empty game object as the parent and just use that new parent transform when you want to rotate transform.parent.LookAt(otherTransform)
So now when you instantiate the object, right after the instantiating call, use the look at function and it should point in the correct direction. As for the laser moving in the correct direction, you want to move in the direction of the vector returned from targetTransform.position - transform.position
. That vector contains the length/distance along with the direction.
You are applying the velocity of the rigidbody as the move direction in the start method, but the velocity is equal to zero when you instantiate the object, so you won’t get anything out of doing that. Also adding force just in the start method will only add the said force once and add force is usually applied over multiple frames so as to accelerate the rigidbody.
If dropping right in front of your alien means that the laser’s beams fall you might want to uncheck “Use Gravity” on the rigidbody. If you want the beam to move at a constant speed in the initial direction of the player when instantiated, then in the start function, you can set the velocity of the rigidbody and keep applying the same velocity. So you would save the initial direction dir = (targetPos - startPos).normalized
with a normalized vector (1 unit in length) and apply that to your velocity with the speed you want velocityToSave = dir * speed
and apply it on each update frame, or you can just apply it once and see how forces run their course
Hope that helps
haha didn't mean to make you the answer I figured it out, the box collider was acting strange when it rotated because it was so small. Thanks anyway though!
Your answer
Follow this Question
Related Questions
How to make a random object generator that responds to simple touch? 0 Answers
Error CS1525: Unexpected symbol 'void' 1 Answer
How to display random questions without repeating the previous? 2 Answers
Brainstorming Optimization 0 Answers
My player character is jumping infinitely and it didn't use to do that... 0 Answers