Question by
unity_BHOQfF3QP7fplA · Dec 27, 2019 at 01:44 PM ·
2d-platformershooting
2D Shooting to target position (same bullet speed regardless of target's and gun's position)
Hi I have this script and I want same bullet speed regardless of target' and gun's position. This script does when target is closer to gun bullet is slower, and when target is further to gun bullet is faster. Can you please tell me what to do? Thanks. Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
public float moveSpeed = 10f;
Rigidbody2D rb;
Vector2 moveDirection;
Player target;
void Start()
{
target = GameObject.FindObjectOfType<Player>();
rb = GetComponent<Rigidbody2D>();
moveDirection = (target.transform.position - transform.position).normalized * moveSpeed;
rb.velocity = new Vector2(moveDirection.x, moveDirection.y);
Destroy(gameObject, 3f);
}
}
Comment
Your answer