Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PolyvivalStudios · May 24, 2018 at 06:21 PM · scripting problemcursorgun movement

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
       }
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PolyvivalStudios · May 24, 2018 at 07:41 PM 0
Share

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.

avatar image Tobychappell PolyvivalStudios · May 25, 2018 at 06:41 PM 0
Share

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?

avatar image PolyvivalStudios · May 25, 2018 at 06:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges