1

I checked through Youtube, Google and previous answers but can't get my game to shoot towards the mouse. The player rotates to follow the mouse and when he shoots it's either inaccurate or sometimes shooting in the other direction. I really don't want to send a huge amount of code but feel as I've mixed code from different tutorials it might be best for you to see it although I kinda organised it, so sorry and thanks in advance

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public int playerId = 0;
    public bool useController;

    public float Curserspeed = 5f;
    public Animator animator;
    public float speed;
    public GameObject crossHair;

    public GameObject bulletPrefab;

    private Vector2 moveVelocity;

    Vector3 movement;
    Vector3 aim;
    bool isAiming;
    bool endOfAiming;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        ProcessInputs();
        AimAndShoot();
        Animate();
        Move();

        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Curserspeed* Time.deltaTime);
    }

    private void ProcessInputs() {
        if (useController) {
            movement = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0.0f);
            aim = new Vector3(Input.GetAxisRaw("MoveHorizontal"), Input.GetAxisRaw("MoveVertical"), 0.0f);
            aim.Normalize();
            isAiming = Input.GetButton("Fire1");
            endOfAiming = Input.GetButtonUp("fire1");
        }
        else {
            movement = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0.0f);
            Vector3 mouseMovement = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0.0f);
            aim = aim + mouseMovement;
            if (aim.magnitude > 0f)
            {
                aim.Normalize();
            }
            isAiming = Input.GetButton("Fire1");
            endOfAiming = Input.GetButtonUp("Fire1");
        }

        if (movement.magnitude > 1.0f)
        {
            movement.Normalize();
        }
    }
    private void Move()
    {
        transform.position = transform.position + movement * speed * Time.deltaTime;
    }
    private void Animate()
    {

        animator.SetFloat("vmovement", Input.GetAxisRaw("Horizontal"));
        animator.SetFloat("movement", Input.GetAxisRaw("Vertical"));
        animator.SetFloat("magnitude", movement.magnitude);
    }

    private void AimAndShoot()
    {
        Vector2 shootingDirection = new Vector2(aim.x, aim.y);
        if (aim.magnitude > 0.0f)

            shootingDirection.Normalize();
            if (Input.GetButtonDown("Fire1")) {
                GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);

                bullet.GetComponent<Rigidbody2D>().velocity = shootingDirection * 3.0f;
                bullet.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
                Destroy(bullet, 3.0f);
            }
int main()
{
    return 0;
}

`

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
ColdCode
  • 11
  • 2
  • Why are you using mouse motion to calculate your aiming point rather than just the raw mouse X and mouse Y position? [`Input.mousePosition`](https://docs.unity3d.com/ScriptReference/Input-mousePosition.html) – Draco18s no longer trusts SE Dec 22 '18 at 22:55
  • thanks for helping and answering quickly .tried everything not sure exactly if i edited it properly or not but cant get it working not sure where to make the change and what all needs changed hard to remember what everything means im like brand new to this so sorry but thanks for your help – ColdCode Dec 26 '18 at 22:11

1 Answers1

0

if your player is looking at the mouse, and you are trying to shoot there, this seems a little complicated...

you only need to do two things once you instantiate your bullet: immediately rotate it so it faces the same direction as the player. (this can be done via, transform.rotation= playerobject.rotation) apply forward force. (since the object is facing where your player was facing, and therefore aiming when you 'fired' the projectile, you dont need to aim, just send it on its forward trajectory)

transform.forward will work here.

Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • thank you for the answer as im painfully new i wasnt succesful implamenting the change you suggested whether i did it wrong or not i dont know:( – ColdCode Dec 26 '18 at 22:10