Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DioVoid · Jul 03, 2014 at 03:20 PM · c#rotationglitch

Camera rotation C# script problem

Can anyone tell me plz, why is this C# script not working. Different glitches, and camera rotates always different directions, and really laggy. I need to fix this entire script.

using UnityEngine; using System.Collections;

 public class Cam1 : MonoBehaviour
 {
     public Transform cam;
     public float sp;
     public Vector3 mouseDelta = Vector3.zero;
     private Vector3 lastMousePosition = Vector3.zero;
     
     void Update()
     {
         mouseDelta = Input.mousePosition - lastMousePosition;
         
         lastMousePosition = Input.mousePosition;
 
         float x = mouseDelta.x;
         float y = mouseDelta.y;
         
         if (y < 0)
         {
             cam.Rotate(x, 0, 0);
         }
         if (y > 0)
         {
             cam.Rotate(-x, 0, 0);
         }
         if (x > 0)
         {
             cam.Rotate(0, y, 0);
         }
         if (x < 0)
         {
             cam.Rotate(0, -y, 0);
         }
     }
 }
Comment
Add comment · Show 2
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 Josh Naylor ♦♦ · Jul 03, 2014 at 03:21 PM 0
Share

what are you trying to achieve?

avatar image DioVoid · Jul 03, 2014 at 05:37 PM 0
Share

To make mouse look

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Gunorgunorg · Jul 03, 2014 at 07:30 PM

You need to make a transform for the mouse position to be the target, and one line will make it look:

 transform.LookAt(target);

It took a while for me to figure this out so that the camera stayed still and looked at my player.

Comment
Add comment · 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
0

Answer by Xyexs · Jul 03, 2014 at 07:34 PM

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour {

     public float mouseSensitivityRightLeft = 5f;
     public float mouseSensitivityUpDown = 5f;
     
     float verticalRotation = 0;
     float verticalVelocity=0;
     // Use this for initialization
     void Start () {
     
     }
     // Update is called once per frame
     void Update () {
         
         //Rotation

             //right/left
         float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivityRightLeft;
         transform.Rotate(0,rotLeftRight,0);
         
             //up/down
 
         float rotUpDown = Input.GetAxis ("Mouse Y") * mouseSensitivityUpDown;
         transform.Rotate(-rotUpDown,0,0);
     }
 }

I used this just a few minutes ago. It works fine.

Comment
Add comment · 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
0

Answer by arky25 · Jul 03, 2014 at 07:54 PM

Instead of using Input.mousePosition, use Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y"), so something like this:

 public class Cam1 : MonoBehaviour
     {
 
     public Transform cam;
 
     public float sp;
     public float speedX;
     public float speedY;
     void Update()
     {
      
     float x = Input.GetAxis("Mouse X") * Time.deltaTime * speedX;
     float y = Input.GetAxis("Mouse Y") * Time.deltaTime * speedY;
      
     if (y < 0)
     {
          cam.Rotate(x, 0, 0);
     }
     if (y > 0)
     {
          cam.Rotate(-x, 0, 0);
     }
     if (x > 0)
     {
          cam.Rotate(0, y, 0);
     }
     if (x < 0)
     {
          cam.Rotate(0, -y, 0);
     }
     }
     }


Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Flip over an object (smooth transition) 3 Answers

C# GameObject Reverses Z Rotation 0 Answers

C# Rotate GameObjects Regardless of List Size 2 Answers

C# Need Help Refining Ricochet Script 1 Answer

Player lives script help 1 Answer


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