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 awplays49 · Jan 04, 2015 at 06:30 PM · mousedistancelooksensitivitydelta

Look rotation is "Snappy"

adding this script to a camera

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     public int Sensitivity;
 
     public float MaxDistance;
     private float MouseY;
     private float MouseYSet;
     private float CameraAngleX;
     private float CameraY;
     private float CameraZ;
 
     void Start () {
     }
     
     void Update () {
         // Look controls
         MouseY -= Input.GetAxisRaw ("Mouse Y");
         CameraY = Mathf.Clamp (CameraY + (MouseY - MouseYSet) * Sensitivity * 10, transform.parent.lossyScale.y / 2, transform.parent.lossyScale.y / 2 + MaxDistance);
         CameraZ = Mathf.Clamp (CameraZ + (MouseY - MouseYSet) * Sensitivity * 10, -MaxDistance, -MaxDistance / 4 * 3);
         if (MouseYSet != MouseY)
         {
             transform.localPosition = new Vector3 (0, CameraY, CameraZ);
             transform.LookAt (transform.parent.position);
             MouseYSet = MouseY;
         }
     }
 }

and this to a player

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public int Speed;
     public int RunSpeed;
     public int Sensitivity;
     public int JumpHeight;
     public int Force;
 
     private float TerrainHeight;
     private float TerrainHeightSet;
     private float PlayerFoot;
     private float MouseX;
     private float MouseXSet;
 
     private bool Jumping;
     private bool Running;
 
     void Start () {
         Jumping = false;
         Running = false;
     }
 
     void Update () {
         PlayerFoot = transform.position.y - transform.lossyScale.y / 2;
         TerrainHeight = Terrain.activeTerrain.SampleHeight (transform.position);
 
         // Translation controls
         if (Input.GetKey (KeyCode.W) && Running == false)
         {
             transform.position += transform.forward * Speed * 20 * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.S) && Running == false)
         {
             transform.position -= transform.forward * Speed * 20 * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.D) && Running == false)
         {
             transform.position += transform.right * Speed * 20 * Time.deltaTime;
         }
         if (Input.GetKey (KeyCode.A) && Running == false)
         {
             transform.position -= transform.right * Speed * 20 * Time.deltaTime;
         }
 
         // Run controls
         if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.Q))
         {
             Running = true;
         }
         if (Input.GetKey (KeyCode.W) && Running == true)
         {
             transform.position += transform.forward * RunSpeed * 20 * Time.deltaTime;
         }
         if (Input.GetKeyUp (KeyCode.Q) && Input.GetKeyUp (KeyCode.W))
         {
             Running = false;
         }
 
         // Rotation controls
         MouseX += Input.GetAxisRaw ("Mouse X");
 
         if (MouseXSet != MouseX)
         {
             transform.Rotate (new Vector3 (0,(MouseX - MouseXSet) * Sensitivity * 10, 0));
             MouseXSet = MouseX;
         }
 
         // Jump controls
         if (Input.GetKey (KeyCode.Space) && PlayerFoot < TerrainHeight + 1 && PlayerFoot < TerrainHeight + JumpHeight * 10)
         {
             TerrainHeightSet = TerrainHeight;
             StartCoroutine (JumpDelay ());
         }
         if (Jumping == true)
         {
             rigidbody.velocity = new Vector3 (0, (Force * 10), 0);
         }
         if (Input.GetKeyUp (KeyCode.Space))
         {
             Jumping = false;
         }
         if (PlayerFoot >= TerrainHeightSet + JumpHeight * 10)
         {
             Jumping = false;
         }
         if (Jumping == false)
         {
             rigidbody.velocity = new Vector3 (0, -(Force * 20), 0);
         }
 
         // Keep player upright
         if (PlayerFoot < TerrainHeight + 1)
         {
             transform.position += Vector3.up * (TerrainHeight - PlayerFoot);
         }
     }
 
     // Check if player really wants to jump
     IEnumerator JumpDelay () {
         yield return new WaitForSeconds (0.05f);
         if (Input.GetKey (KeyCode.Space))
         {
             Jumping = true;
         }
         StopCoroutine (JumpDelay ());
     }
 } 

it works fine with the looking left and right (not snappy) but vertical camera rotation is strange. it has about 20 different positions for some reason, almost like the positions are ints, but theyre not. this isnt a HUGE issue, but id like to eventually get it fixed. ive already had a look but theres nothing i can spot right now. if anyone could have a look, id appreciate it. sensitivity i set to 9 on both the camera and player, and i set maxdistance to 50.

Comment
Add comment · Show 6
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 AcE_fLoOdEr · Jan 04, 2015 at 11:09 PM 0
Share

Considered using the First Person Controller that comes with unity?

avatar image EggQuiz857 · Jan 04, 2015 at 11:15 PM 0
Share

You wasted your time. the first person controller with unity is customizable and simpler. That is so much code.

avatar image jpthek9 · Jan 05, 2015 at 01:16 AM 0
Share

$$anonymous$$other hugger who the heck is gonna read through all that? You gotta show exactly where you think the issue is or people like me aren't even gonna read it.

avatar image HYPERSAVV · Jan 05, 2015 at 01:39 AM 0
Share

First off, props. Just because a script exists does not mean you have to use it.

To the people who commented saying there was too much code, incorrect. His problem clearly lies in the first script he posted. The second script is extra, a nice to have. Not to mention, reading the comments (again, props) will prune out the code you're not interested in.

Now...your problem, why aren't you just rotating your camera around the x-axis? I'm a bit confused as to why you're moving the transform and not rotating in the camera script, while you do in the player script.

avatar image AcE_fLoOdEr · Jan 05, 2015 at 01:42 AM 1
Share

@HYPSERSAVV

Hey man, you're right. $$anonymous$$aking your own script is an accomplishment. But in the real world, you want to take what's already available to you and modify it to your needs. Otherwise, I will just go write my own program$$anonymous$$g language than just using what's already available.

Let's be real, OP wasted lots of time on something that's out there and free to use by default. Just my two bits.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by awplays49 · Jan 05, 2015 at 12:49 PM

It's ok guys. I already deleted the script from my game. You were all right. It was too much code. Thanks for the help.

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

27 People are following this question.

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

Related Questions

Mouse look changes direction after going over center of screen 0 Answers

Mouse look Y axis sensitivity doesn't change 0 Answers

xRotation is not working,xRotation is not working why? 1 Answer

Camera movement independent of player control 1 Answer

How to convert the MouseLook(Script) to a java script. 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