• 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 Warriorubi · May 31, 2012 at 04:13 PM · cameraerrorplayermove

Player move and camera problem

Hi everyone, I've started to use Unity3D one week ago... I've done a lot of trials before ask this... I've change code a lot of times... I've been able to solve some errors... But i didn't understand where is this bug... It works fine at start but when you move some mouse and keyboard, it gives some problems... This JavaScript have to be applicated to a Player (Capsule) with behind a Main Camera (used this to semplify work)... So if someone can help me, A lot of thanks!!!

Code:

 private var direction : Vector3; // Direction of player to know next position
  
 function Update () {
     Camera.main.transform.LookAt(transform); // Normal LookAt function
     direction = Camera.main.transform.forward; // I've set the direction of player like camera's one.
    
     transform.Translate(direction * Input.GetAxis("Vertical"),Space.World); // Up/Down move player near/further than camera
     transform.Translate(direction.z * Input.GetAxis("Horizontal"),0,direction.x * -Input.GetAxis("Horizontal"),Space.World); // Horizontal Movement
  
     var mouX : float = Input.GetAxis("Mouse X");
     var mouY : float = Input.GetAxis("Mouse Y");
    
     transform.RotateAround(transform.position,Vector3.up,mouX); // Move Player and camera horizontally by mouse
     var euler = Camera.main.transform.eulerAngles.x; // See camera's rotation to put a limit
     if(euler < 180){
         if((euler < 50) || (mouY > 0)) // I don't want a lot of vertical vision
             Camera.main.transform.RotateAround(transform.position,Vector3.right,-mouY); // To look Around
     }else{
         if((euler > 330) || (mouY < 0))
             Camera.main.transform.RotateAround(transform.position,Vector3.right,-mouY);  
     }
    
     Screen.lockCursor = true; // Normal Cursor locked function
 }
Comment
Add comment · Show 3
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 Warriorubi · May 31, 2012 at 03:08 PM 0
Share

Sorry for my bad English...

avatar image hijinxbassist · May 31, 2012 at 07:27 PM 0
Share

@Warriorubi You say "it gives some problems" but never specify what the problems are. Can you explain what is going wrong. Is the camera parented to the player? Its most likely a conflict between your Euler limits and the LookAt function.

avatar image Warriorubi · May 31, 2012 at 07:57 PM 0
Share

@hijinxbassist I didn't think about it... Yes.. The camera is parented to the player... I try to do an explication of the problem but its better see it by yourself... So, when i move the mouse up (this without keyboard input) it turns correctly and shows me the down (for the LookAt function)... but if i before turn the mouse left or right by 90° and then i try to go up with the mouse... i can't.. i hope that you can understand...

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Wolfram · May 31, 2012 at 09:16 PM

Vector3.right will always rotate around the world X axis. Once you rotate your character around Y, this is no longer what you want.

So instead, rotate around the local X axis of your character:

 Camera.main.transform.RotateAround(transform.position,transform.right,-mouY);

Note on the other hand, the use of Vector3.up is generally fine, since you usually still want to rotate around world Y instead of local Y, even if your character is facing up or down.

Comment
Add comment · Show 1 · 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 Warriorubi · May 31, 2012 at 10:23 PM 0
Share

Only one word can make me feel crazy. Unbelievable. Thanks a lot!!! You have solved a two days' problem. Now my head is again free... I can think again!!!

avatar image
0

Answer by umer772 · Aug 17, 2017 at 05:12 AM

when player moves grass on terrain removed after some distance and no grass seen on terrain after some distance pls help to sovle it????

Comment
Add comment · Show 1 · 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 Wolfram · Aug 17, 2017 at 09:47 AM 0
Share

Please post this as a new, separate question. It is unrelated to this question, and will also not be seen here (except by the two people subscribed to this question...).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Camera problem 2 Answers

How to make a camera that moves with the player? 1 Answer

Building Game w/ Scriptable Objects 1 Answer

Erratic movement problem 2 Answers

3rd person player 0 Answers


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