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 ak1617 · Jun 05, 2012 at 11:01 PM · controlstopdownmouse-look

Trouble with controls on top down fixed camera game.

Im trying to code a top down game with geometry wars type controls but it It has a single fixed top down camera looking straight down in the middle of the room. I want the player to move around the room freely with the arrow keys and always look in the direction of the mouse. Right now the movement works but the character rotation/always looking toward the mouse is messed up. The character sort of looks at the mouse but it sorta lags and sometimes looks in the wrong direction like if i move the mouse quickly to the opposite side of the player it wont rotate 180 and continue looking at the mouse. I want it to be snappy and always fixed facing the direction of the mouse. Heres what I have so far, I started out by borrowing some code from the Evac-City tutorial. Thanks a lot

 function Update () {    
     FindPlayerInput();
     ProcessMovement();
 }
 //game objects  (variables which point to game objects)
 var objPlayer : GameObject;
 
 //input variables (variables used to process and handle input)
 var inputRotation : Vector3;
 var inputMovement : Vector3;
 var moveSpeed = 100;
 var tempVector : Vector3;
 var tempVector2 : Vector3;
 
 function FindPlayerInput ()
 {
     // find vector to move
     inputMovement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     
     //position of player
     tempVector2 = new Vector3(objPlayer.transform.position.x, 0 , objPlayer.transform.position.z);
     
     tempVector = Input.mousePosition; // find the position of the mouse on screen
     
     tempVector.z = tempVector.y; // input mouse position gives us 2D
     //coordinates, I am moving the Y coordinate to the Z coorindate in temp Vector and setting the Y
     //coordinate to 0, so that the Vector will read the input along the X (left and right of screen) and Z
     //(up and down screen) axis, and not the X and Y (in and out of screen) axis
     tempVector.y = 0;
 }
 
 function ProcessMovement()
 {
     objPlayer.rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
 
     //Debug.Log(tempVector);
     objPlayer.transform.LookAt(tempVector);
     
     objPlayer.transform.rotation.x = 0;
     objPlayer.transform.rotation.z = 0;
     
     objPlayer.transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alpaca of zion · Jun 07, 2012 at 04:51 AM

I think your problem is they you are grabbing the screen cordnance and not converting them to 3d space. This means that the mouse in the lower left hand corner of the screen is at the point 0,0,0 in 3d space, assuming the y axis is set to zero. When the mouse is at the upper left hand corner your point would be at 640,0,480 depending on your resolution. What you need to do is use Camera.ScreenPointToRay and do a raycast to the point where y is 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate an OBJECT along Y axis to look at mouse cursor 0 Answers

Can't use camera.main [SOLVED] 1 Answer

Top down 2d movement without being affected by rotation 2 Answers

Speed Change Doesn't Apply,Why does changing the speed makes the controls wrong 0 Answers

Fluid and Constant Rotation for Space Sim? 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