- Home /
player shoot at cursor/normalize problems 2D,normalize and aim 2D
hello i want my player to shoot at an object following the cursor"aimscript". the problem is that the closer the cursor is to my player the slower the bullet will move. i presume it is because normalize dosnt work with numbers less than 1. does any one have an idea?
public class bulletscript : MonoBehaviour { Vector2 moveDirection; aimscript target; float moveSpeed = 5;
void Start () {
target = GameObject.FindObjectOfType<aimscript>();
moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveDirection.x, moveDirection.y);
// Update is called once per frame
void Update () {
}
}
,hello i want my player to shot at an object following the cursor "aimscript". the problem is that when the cursor is too close to the player, the bullet will move slower. is there a way to normalize up to 1 instead?
public class bulletscript : MonoBehaviour { Vector2 moveDirection; aimscript target; float moveSpeed = 5;
void Start () {
target = GameObject.FindObjectOfType<aimscript>();
moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveDirection.x, moveDirection.y);
Destroy(gameObject, 3f);
Debug.Log(moveDirection);
}
// Update is called once per frame
void Update () {
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612183444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pointing 2 empty game objects towards one point? (Raycast?) 1 Answer
Press button to play "Aim" animation without repeating. 2 Answers
Aiming a shot in Unity 2d? 0 Answers
2D Cursor Aiming 2 Answers
Getting the face direction 2 Answers