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

How to rotate object using mouse moving using C# script?

Discussion in 'Scripting' started by KOR_APUcard, Jul 10, 2019.

  1. KOR_APUcard

    KOR_APUcard

    Joined:
    Jul 21, 2018
    Posts:
    9
    I don't know how to programming this. And my friends too. How to make this script? like a L4D2, HL2 mounted machine gun. Please show me a C# example!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Have you tried googling these four words: unity gun turret mouse

    For me at least three big tutorials come up showing how to do various things like that.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Or failing Kurt's advice: do you have an L4D2, HL2 mounted machine gun for me to try and then implement the code?
     
  4. KOR_APUcard

    KOR_APUcard

    Joined:
    Jul 21, 2018
    Posts:
    9
    I've all tried methods. But not all are satisfactory. And I just implement code similar to L4D2, HL2, but I will not implement it the same.
     
  5. KOR_APUcard

    KOR_APUcard

    Joined:
    Jul 21, 2018
    Posts:
    9
    What I want is that the center of rotation of the camera is at the center of the object. Like a third person camera.
    GameQuestion-Example01.png
    When player is looks around. (Moving camera with mouse)
    GameQuestion-Example01a.png

     
    Last edited: Jul 14, 2019
  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    So what class is this for?

    Anyway, to implement what you are looking for, the following approach may help:
    have the turret object center at theblack center.

    Make camera and player each a child of the main turret and place them locally relative to the turret

    Use lookRotation() to turn the turret towards the pink circle
     
    Kurt-Dekker likes this.
  7. KOR_APUcard

    KOR_APUcard

    Joined:
    Jul 21, 2018
    Posts:
    9
    Okay, thanks.