- Home /
How could i make my gun move where my cursor is?
I have a mounted gun on my vehicle and I would like it to move where my cursor is.
Answer by Tobychappell · May 24, 2018 at 06:40 PM
Edit: After getting more specs I've re-done this answer.
There are 2 scripts, The CameraScript and the PointerScript. The CameraScript needs a reference to a PointerScript. And the PointerScript must be attached to a gameobject that is childed to another gameobject. Their world positions need to be the same. When the PointerScript is selected a frustum gizmo will be drawn that shows the approximate field of view the PointerScript can take
CameraScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraScript : MonoBehaviour
{
public PointerScript myPointer;
private Camera myCamera;
private RaycastHit rayHit;
public Vector3? CurrentPoint { get; private set; }
private void Awake()
{
myCamera = GetComponent<Camera>();
}
// Use this for initialization
void Start ()
{
if (myPointer == null)
{
Debug.LogError(string.Format("'myPointer' was not set, disabling CameraScript on gameobject: ({0})", name));
this.enabled = false;
}
}
// Update is called once per frame
void Update ()
{
CurrentPoint = GetNewPosition();
myPointer.UpdateTarget(CurrentPoint);
}
private Vector3? GetNewPosition()
{
Ray myRay = myCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out rayHit))
{
return rayHit.point;
}
return null;
}
}
PointerScript
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointerScript : MonoBehaviour
{
private Transform tran;
private Transform parentTran;
[Tooltip("Maximum degrees this transform will look either side (along the y-axis) of its parents forward")]
public float yawConeDegrees;
[Tooltip("Maximum degrees this transform will look up and down of its parents forward")]
public float pitchConeDegrees;
private Vector3? currentTarget;
private Vector3 aimDirection;
private float returnTime;
private void Awake()
{
tran = transform;
}
void Start () {
if (tran.parent == null)
{
Debug.LogError(string.Format("This script 'PointerScript' requires a parent, disabling PointerScript on gameobject: ({0})", name));
this.enabled = false;
}
parentTran = tran.parent;
}
void Update ()
{
if (currentTarget.HasValue) // If we have a position, rotate towards it
{
aimDirection = (currentTarget.Value - tran.position).normalized;
Vector3 parentForward = parentTran.forward;
Vector3 yawFlat = aimDirection;
yawFlat.y = 0;
yawFlat = yawFlat.normalized;
Vector3 pitchFlat = aimDirection;
pitchFlat.x = 0;
pitchFlat = pitchFlat.normalized;
float yaw = Vector3.SignedAngle(parentForward, yawFlat, tran.up);
float pitch = Vector3.SignedAngle(parentForward, pitchFlat, tran.forward);
if (Mathf.Abs(yaw) <= yawConeDegrees && Mathf.Abs(pitch) <= pitchConeDegrees)
{
tran.LookAt(currentTarget.Value, parentTran.up);
}
}
}
private void OnDrawGizmosSelected()
{
if (transform.parent != null)
{
Matrix4x4 previous = Gizmos.matrix;
Gizmos.matrix = transform.parent.localToWorldMatrix;
Gizmos.DrawFrustum(Vector3.zero,pitchConeDegrees*2, 5,0, yawConeDegrees / pitchConeDegrees);
Gizmos.matrix = previous;
}
}
public void UpdateTarget(Vector3? newPoint)
{
currentTarget = newPoint;
}
}
UPDATE: The script you provided works, but the gun is turning like crazy. I forgot to mention, that I would like the gun to only move in the z- and y-axis. I've tried to use freezeRotation, but it had no effect.
Is this 3d? Also when you say z and y do you means it's local X and Y? Y axis so it can rotate around vertically (Yaw), X axis so it can rotate up and down (pitch). Did you want a limit of how much it can rotate in each component of x and y?
It's 3D and what you just said, is exactly what I wanted! I would like a 90 X and -90 X and 90 Y, -90 Y Limit.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163347im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Look On Cursor Problem 1 Answer
Can you lock the cursor to specific coordinates? 0 Answers
Box goes through walls 2 Answers
Locking cursor to middle of screen not working 1 Answer
Cursor / MouseOver Glitch 0 Answers