Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How do I make my gun move in a circular form based on where my player is facing.

Discussion in 'Scripting' started by ansonisawesome24, Jun 1, 2020.

  1. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    I want to know how to make my gun move in a circular form based on where my player is facing.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    That doesn't sound too complicated... How many youtube gun / shooting tutorials have you worked through?

    If you get stuck in any coding issues in a tutorial, here is how to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  3. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    I tried looking at stuff on the internet and i didnt know what to search.
     
  4. ansonisawesome24

    ansonisawesome24

    Joined:
    Dec 23, 2019
    Posts:
    13
    I figured something out,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunScript : MonoBehaviour
    6. {
    7.     //public Vector3 offset;
    8.  
    9.     public Transform player;
    10.     public Vector3 pointasdf;
    11.     public Camera xAxisCam;
    12.  
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.     private void LateUpdate()
    18.     {
    19.         var point = xAxisCam.ViewportToWorldPoint(pointasdf);
    20.  
    21.         //transform.position = player.position + offset;
    22.  
    23.         transform.position = point;
    24.     }
    25.     //void Update()
    26.     //{
    27.     //    var pointXYZ = new Vector3(0.5f, 0.5f, 2.0f);
    28.     //
    29.     //    var point = Camera.main.ViewportToWorldPoint(pointXYZ);
    30.     //
    31.     //    transform.position = player.position + offset;
    32.     //
    33.     //    transform.position = point;
    34.     //}
    35. }  
    but this happens when I use this, it is choppy and still rotates, how do i stop it from rotating and make it smoother