How to rotate the player when moving your mouse?











up vote
0
down vote

favorite












What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.



Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.



This is my Code:



#pragma strict

var spinx : int = 0;
var spiny : int = 0;
var spinz : int = 0;

function Update () {
transform.Rotate(spinx, spiny, spinz);
}









share|improve this question
















bumped to the homepage by Community 15 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    0
    down vote

    favorite












    What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.



    Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.



    This is my Code:



    #pragma strict

    var spinx : int = 0;
    var spiny : int = 0;
    var spinz : int = 0;

    function Update () {
    transform.Rotate(spinx, spiny, spinz);
    }









    share|improve this question
















    bumped to the homepage by Community 15 hours ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.



      Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.



      This is my Code:



      #pragma strict

      var spinx : int = 0;
      var spiny : int = 0;
      var spinz : int = 0;

      function Update () {
      transform.Rotate(spinx, spiny, spinz);
      }









      share|improve this question















      What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.



      Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.



      This is my Code:



      #pragma strict

      var spinx : int = 0;
      var spiny : int = 0;
      var spinz : int = 0;

      function Update () {
      transform.Rotate(spinx, spiny, spinz);
      }






      unity3d rotation unityscript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 24 '14 at 12:45









      LearnCocos2D

      58.8k19114209




      58.8k19114209










      asked Oct 24 '14 at 11:03









      Johndii1491

      117




      117





      bumped to the homepage by Community 15 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 15 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          0
          down vote













          this script should rotate your object acccording to mouse position



          using UnityEngine;
          using System.Collections;

          public class MouseLook : MonoBehaviour {

          void Update () {
          Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
          Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
          lookPos = lookPos - transform.position;
          float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

          }
          }





          share|improve this answer




























            up vote
            0
            down vote













            You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.






            share|improve this answer




























              up vote
              0
              down vote













              The following script should rotate your object according to mouse position,



               using UnityEngine;
              using System.Collections;

              public class RotateClass : MonoBehaviour {
              public float horizontalSpeed = 2.0F;
              public float verticalSpeed = 2.0F;
              void Update() {
              float h = horizontalSpeed * Input.GetAxis("Mouse X");
              float v = verticalSpeed * Input.GetAxis("Mouse Y");
              transform.Rotate(v, h, 0);
              }
              }





              share|improve this answer




























                up vote
                0
                down vote













                First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.



                private Vector3 MousePositionViewport = Vector3.zero;
                private Quaternion DesiredRotation = new Quaternion();
                private float RotationSpeed = 15;

                void Update () {
                MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                if (MousePositionViewport.x >= 0.6f) {
                DesiredRotation = Quaternion.Euler (0, 90, 0);
                } else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
                DesiredRotation = Quaternion.Euler (0, 270, 0);
                }else {
                DesiredRotation = Quaternion.Euler (0, 180, 0);
                }
                transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
                }





                share|improve this answer





















                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26546664%2fhow-to-rotate-the-player-when-moving-your-mouse%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  0
                  down vote













                  this script should rotate your object acccording to mouse position



                  using UnityEngine;
                  using System.Collections;

                  public class MouseLook : MonoBehaviour {

                  void Update () {
                  Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
                  Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
                  lookPos = lookPos - transform.position;
                  float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
                  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

                  }
                  }





                  share|improve this answer

























                    up vote
                    0
                    down vote













                    this script should rotate your object acccording to mouse position



                    using UnityEngine;
                    using System.Collections;

                    public class MouseLook : MonoBehaviour {

                    void Update () {
                    Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
                    Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
                    lookPos = lookPos - transform.position;
                    float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
                    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

                    }
                    }





                    share|improve this answer























                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      this script should rotate your object acccording to mouse position



                      using UnityEngine;
                      using System.Collections;

                      public class MouseLook : MonoBehaviour {

                      void Update () {
                      Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
                      Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
                      lookPos = lookPos - transform.position;
                      float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
                      transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

                      }
                      }





                      share|improve this answer












                      this script should rotate your object acccording to mouse position



                      using UnityEngine;
                      using System.Collections;

                      public class MouseLook : MonoBehaviour {

                      void Update () {
                      Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
                      Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
                      lookPos = lookPos - transform.position;
                      float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
                      transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 24 '14 at 13:08









                      Milad Qasemi

                      2,2132816




                      2,2132816
























                          up vote
                          0
                          down vote













                          You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.






                          share|improve this answer

























                            up vote
                            0
                            down vote













                            You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.






                            share|improve this answer























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.






                              share|improve this answer












                              You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 26 '14 at 12:36









                              Jordi Knol

                              315




                              315






















                                  up vote
                                  0
                                  down vote













                                  The following script should rotate your object according to mouse position,



                                   using UnityEngine;
                                  using System.Collections;

                                  public class RotateClass : MonoBehaviour {
                                  public float horizontalSpeed = 2.0F;
                                  public float verticalSpeed = 2.0F;
                                  void Update() {
                                  float h = horizontalSpeed * Input.GetAxis("Mouse X");
                                  float v = verticalSpeed * Input.GetAxis("Mouse Y");
                                  transform.Rotate(v, h, 0);
                                  }
                                  }





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    The following script should rotate your object according to mouse position,



                                     using UnityEngine;
                                    using System.Collections;

                                    public class RotateClass : MonoBehaviour {
                                    public float horizontalSpeed = 2.0F;
                                    public float verticalSpeed = 2.0F;
                                    void Update() {
                                    float h = horizontalSpeed * Input.GetAxis("Mouse X");
                                    float v = verticalSpeed * Input.GetAxis("Mouse Y");
                                    transform.Rotate(v, h, 0);
                                    }
                                    }





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      The following script should rotate your object according to mouse position,



                                       using UnityEngine;
                                      using System.Collections;

                                      public class RotateClass : MonoBehaviour {
                                      public float horizontalSpeed = 2.0F;
                                      public float verticalSpeed = 2.0F;
                                      void Update() {
                                      float h = horizontalSpeed * Input.GetAxis("Mouse X");
                                      float v = verticalSpeed * Input.GetAxis("Mouse Y");
                                      transform.Rotate(v, h, 0);
                                      }
                                      }





                                      share|improve this answer












                                      The following script should rotate your object according to mouse position,



                                       using UnityEngine;
                                      using System.Collections;

                                      public class RotateClass : MonoBehaviour {
                                      public float horizontalSpeed = 2.0F;
                                      public float verticalSpeed = 2.0F;
                                      void Update() {
                                      float h = horizontalSpeed * Input.GetAxis("Mouse X");
                                      float v = verticalSpeed * Input.GetAxis("Mouse Y");
                                      transform.Rotate(v, h, 0);
                                      }
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 19 at 11:50









                                      Codemaker

                                      637312




                                      637312






















                                          up vote
                                          0
                                          down vote













                                          First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.



                                          private Vector3 MousePositionViewport = Vector3.zero;
                                          private Quaternion DesiredRotation = new Quaternion();
                                          private float RotationSpeed = 15;

                                          void Update () {
                                          MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                                          if (MousePositionViewport.x >= 0.6f) {
                                          DesiredRotation = Quaternion.Euler (0, 90, 0);
                                          } else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
                                          DesiredRotation = Quaternion.Euler (0, 270, 0);
                                          }else {
                                          DesiredRotation = Quaternion.Euler (0, 180, 0);
                                          }
                                          transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
                                          }





                                          share|improve this answer

























                                            up vote
                                            0
                                            down vote













                                            First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.



                                            private Vector3 MousePositionViewport = Vector3.zero;
                                            private Quaternion DesiredRotation = new Quaternion();
                                            private float RotationSpeed = 15;

                                            void Update () {
                                            MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                                            if (MousePositionViewport.x >= 0.6f) {
                                            DesiredRotation = Quaternion.Euler (0, 90, 0);
                                            } else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
                                            DesiredRotation = Quaternion.Euler (0, 270, 0);
                                            }else {
                                            DesiredRotation = Quaternion.Euler (0, 180, 0);
                                            }
                                            transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
                                            }





                                            share|improve this answer























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.



                                              private Vector3 MousePositionViewport = Vector3.zero;
                                              private Quaternion DesiredRotation = new Quaternion();
                                              private float RotationSpeed = 15;

                                              void Update () {
                                              MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                                              if (MousePositionViewport.x >= 0.6f) {
                                              DesiredRotation = Quaternion.Euler (0, 90, 0);
                                              } else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
                                              DesiredRotation = Quaternion.Euler (0, 270, 0);
                                              }else {
                                              DesiredRotation = Quaternion.Euler (0, 180, 0);
                                              }
                                              transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
                                              }





                                              share|improve this answer












                                              First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.



                                              private Vector3 MousePositionViewport = Vector3.zero;
                                              private Quaternion DesiredRotation = new Quaternion();
                                              private float RotationSpeed = 15;

                                              void Update () {
                                              MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
                                              if (MousePositionViewport.x >= 0.6f) {
                                              DesiredRotation = Quaternion.Euler (0, 90, 0);
                                              } else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
                                              DesiredRotation = Quaternion.Euler (0, 270, 0);
                                              }else {
                                              DesiredRotation = Quaternion.Euler (0, 180, 0);
                                              }
                                              transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Oct 19 at 9:33









                                              Noblight

                                              1165




                                              1165






























                                                   

                                                  draft saved


                                                  draft discarded



















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26546664%2fhow-to-rotate-the-player-when-moving-your-mouse%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Tonle Sap (See)

                                                  I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                                                  Guatemaltekische Davis-Cup-Mannschaft