Question by
AnaerobicCombustion · Apr 11, 2017 at 05:38 PM ·
c#mouseshooting2d animation
Shooting a bullet towards the mouse
I would like to be able to shoot a bullet from the player character in a straight line to where the mouse was when it was clicked, and continue to move in that direction once the bullet has passed where the mouse was. I can currently get the bullets to spawn on the player character, but cannot rotate them towards the mouse and only move them a right angles. Ideally, the code for instantiating and rotating the bullet would be in the player controller script, but the bullet's movement in the bullet's script. This is my current code in my player controller script:
public class playerController : MonoBehaviour {
public float speed = 5f;
public GameObject shot;
public float nextfire = 0.0f;
public float firerate = 0.1f;
private Vector3 moveVelocity;
private Rigidbody rb;
void FixedUpdate()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
moveVelocity.Set (h, v, 0.0f);
moveVelocity = moveVelocity / (Mathf.Sqrt(2)) * speed * Time.deltaTime;
rb = GetComponent<Rigidbody> ();
rb.MovePosition (rb.position + moveVelocity);
}
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextfire)
{
nextfire = Time.time + firerate;
// Instantiate (shot, rb.position, rb.rotation);
}
}
}
I have looked at other, similar questions and found the replies to either be out of date or unhelpful. Please help!
Comment