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 digitalsoap · Mar 20, 2011 at 06:10 AM · objects-movement

Player Movement based on x/y screen coordinates, not world coordinates

I'm trying to develop a top-down shooter, however I'm having some trouble figuring out how to control player movement based on x/y screen coordinates instead of world coordinates. While I have most of the movement worked out, I can't figure out how to apply the 2D movement in 3D space - I've looked into the functions to do this in Unity, and while I understand them in theory the examples provided, as well as a few days of research, hasn't yielded an answer I can understand or break down - maybe there are better resources out there, but I can't find them.

Please help!

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 Meltdown · Mar 20, 2011 at 09:43 AM 0
Share

I think you are going about this the wrong way. Unity3D is built for making/manipulating objects in 3D space, is there any particular reason why you want to do movement based on screen co-ordinates ins$$anonymous$$d of 3d space?

avatar image digitalsoap · Jul 04, 2011 at 01:23 AM 0
Share

$$anonymous$$eltdown: Imagine a game like SmashTV, where the position of the mouse controls the character's rotation (eg always facing the mouse), but "forward" should always be up on screen +x, down on screen -x, left on -y and right on +y.

Thanks for the tips everyone, still working on figuring it out however.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by loopyllama · Mar 20, 2011 at 10:52 AM

You can use the x y of the screen. When you figure out your object's movement in screen space then you can convert it to world space and move your character in worldspace.

Camera.ScreenToWorldPoint

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html

When doing this you must set the z properly. It is the distance of your camera to your object in worldspace. If you fail to set the z properly you will get an incorrect result.

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 Owen-Reynolds · Mar 20, 2011 at 04:29 PM

The trick is to think of what you want it to look like, then how to do it. It sounds like you want the player to always be facing "North," in a top view.

A really cheesy way to do that is to child the camera to the player, aim it down (maybe x rotation of 70, for a little look ahead) and move it until you get a good field of view.

Later, if you're feeling ambitious, add a script that does the same thing, but now you can program some bounce, a scroll wheel, screen shake... .

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 Robo_Chiz · Sep 01, 2012 at 06:19 PM

I had trouble with this. I created a small cuboid and and positioned it inside the player. I Gave it the cuboid a Character Controller. Then I gave it the script at the bottom of this answer, and then parented the Player Object to the Cuboid. The Cuboid should handle the movement and the player should follow it because it is a child. (Side Note: You may need to rotate the cuboid to get the move in the right direction. )

 private var moveDirection  : Vector3  = Vector3 .zero ;
 
 var speed  : float = 5;
 
 
 function Update ()
 
 {
 
     var controller  : CharacterController  = GetComponent (CharacterController );
 
  
 
     moveDirection  = transform.TransformDirection (Input .GetAxis ("Horizontal"), 0, Input .GetAxis ("Vertical"));
 
     moveDirection  *= speed ;
 
  
 
     controller .Move (moveDirection  * Time .deltaTime );
 
 }
 
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

2 People are following this question.

avatar image avatar image

Related Questions

Object not moving according to the meshes..? 0 Answers

Orbiting objects appear stretched 1 Answer

My object only works correctly when selected on hierachy 0 Answers

How to have a character move on its own? 0 Answers

Rythm Game - Move objects synchronized with the music 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