- Home /
Problem with bullet moving in straight line
I thought this problem would be straightforward but I am running into a strange issue.
I am making a tower defense game and so I have towers shooting bullets at incoming enemies. When an enemy enters the towers "radius of detection" I find the direction the bullet needs to travel by subtracting the enemies position from the tower's position and normalize that vector. I then create a bullet prefab and pass that direction in. To move the bullet, I have the following code in the update function:
transform.position += direction * speed * Time.deltaTime;`
But when the game runs, it seems that the movement has been decomposed into the corresponding axes. For example if the resulting vector of direction * speed * Time.deltaTime
were (.5, -.8, 0), it would first move horizontally right .5, then move down vertically .8. It resembles a stair shape opposed to a straight line. I have no idea why, is there something I am misunderstanding?
Answer by Jon_Brant · Apr 17, 2018 at 02:06 AM
Do you have your code that moves the bullet in Update()? Try moving it to FixedUpdate()
Answer by MarioSantoso · Apr 17, 2018 at 02:12 AM
Try doing this (pseudo code)
Enemy enter tower's detection radius.
Get the enemy transform.
Instantiate bullet prefab.
Use LookAt function to make the bullet look at the enemy.
Use "transform.Translate(Vector3.forward moveSpeed Time.deltaTime, Space.Self);" to move the bullet toward the enemy.
Answer by shadowpuppet · Apr 17, 2018 at 07:04 PM
LookAt as in #4 in previous comment.If bullet has a rigidbody addFOrce to move it in direction of lookAt
GameObject Bullet = Instantiate (bullet, transform.position , transform.rotation)as GameObject;
Bullet.GetComponent<Rigidbody> ().AddForce (transform.forward * throwDistance);
Your answer
![](https://koobas.hobune.stream/wayback/20220612160918im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
AddForce/ForceMode.Impulse 1 Answer
shoot forward and comeback 0 Answers
How to get character movement done with box collider 2d? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers