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. Dismiss Notice

How to rotate right and left

Discussion in 'Scripting' started by unity_P9vZszLH1E-mTw, Oct 11, 2018.

  1. unity_P9vZszLH1E-mTw

    unity_P9vZszLH1E-mTw

    Joined:
    Oct 11, 2018
    Posts:
    2
    Hi guys, can u tell me how should i do to rotate the camera (in-game) when i move the mouse right or left? Here there is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Sparo : MonoBehaviour {
    6.     public float velocita = 10.0f;
    7.     public float potenzaProiettile = 1000.0f;
    8.     public Rigidbody proiettile;
    9.     public Transform arma;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.        
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.         //programmo i movimenti
    20.         float movimentoOrizzontale = Input.GetAxis("Horizontal") * (velocita * Time.deltaTime);
    21.         float movimentoVerticale = Input.GetAxis("Vertical") * (velocita * Time.deltaTime);
    22.  
    23.         transform.Translate(new Vector3(movimentoOrizzontale, 0.0f, movimentoVerticale));
    24.  
    25.  
    26.         if (Input.GetButtonUp("Fire1"))
    27.         {
    28.             Rigidbody sparato = Instantiate(proiettile, arma.position , Quaternion.identity) as Rigidbody;
    29.             sparato.AddForce(Vector3.forward * potenzaProiettile);
    30.         }
    31.        
    32.     }
    33. }
    34.  


    This is the code to make my Camera shoot a sphere when i press the left mouse button... and i neet to rotate my camera too...
     
  2. zakdank

    zakdank

    Joined:
    Jan 25, 2011
    Posts:
    46
    Not at my PC but you can use use Input.GetAxis("Mouse X") to get the current movement of the mouse left and right and then you just need to += that onto the camera transforms rotation on the z axis.
     
    unity_P9vZszLH1E-mTw likes this.