Question by
SuperSparkplug · Dec 19, 2017 at 01:25 AM ·
mousepositionaiming
Making a First Person Aiming and Reticule with Mouse Postion.
Hi there,
Currently I have sequence in my game where I want the player to aim with the house and shoot things. The player's position is constant and doesn't move. I'd also like to have an aiming reticule to make it easy to see where they're shooting.
I have an aiming script right now. However, it's super wonky and I'm not quite sure how to fix it. It ends up going all over the place and I don't know how I would limit it so it becomes more manageable. Any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : MonoBehaviour {
public float magnifier = 0.5f;
public Rigidbody shotPrefab;
public Transform barrelEnd;
public float shotSpeed = 500;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
float midPoint = (transform.position - Camera.main.transform.position).magnitude * magnifier;
//MousePosition = Input.mousePosition;
transform.LookAt (mouseRay.origin + mouseRay.direction * midPoint);
if (Input.GetButtonDown("Fire1"))
{
Rigidbody shotInstance;
shotInstance = Instantiate(shotPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
shotInstance.AddForce(barrelEnd.forward * shotSpeed);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612144831im_/https://answers.unity.com/themes/thub/images/avi.jpg)