• 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 lvl9hero · Jun 19, 2014 at 12:50 AM · movementplayermouseclickmove to

Moving player to mouse click, falling through scene

Hi

So I have a Cube/box collider as my Player.

I have already applied a JS onto it to make it constantly move forward in my scene. The ground is just a cube/box collider scaled out.

Here's what I have already on the object:

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
     transform.Translate(15 * Vector3.forward * Time.deltaTime, Space.World);
 }


Now I tried to attach a new second script to make the object move to where ever I click at on the ground (while it's still constantly moving forward)...

but it makes my object instead start falling through the ground and moves upward. As you can immediately see I have zero coding knowledge but I'm trying to learn :)

 #pragma strict
 
 private var target:Vector3;
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             target.z = transform.position.z;
         }
         transform.position = Vector3.MoveTowards(transform.position, target, 10 * Time.deltaTime);
 }



What i'm trying to achieve is to setup a standard top down scrolling airplane shooter game. That's why I have the constant-move-forward applied so it moves through the scene/level. I want to be able to move the player around within the screen while the level scene "scrolls" through.

If anyone can even help show me a better way to write this I'd be so appreciative. Thanks!

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 xandermacleod · Jun 20, 2014 at 12:41 AM

To understand this better it would be helpful to know if your camera is angled in such a way that Positive Y is the forward direction of your space ship or if it's positive Z.

What is likely happening is that because Input.mousePosition is a 2d vector, your 3d vector has its Y value where its Z value should be.

That would be my guess.

To rectify this you need something like:

 #pragma strict
  
 private var target:Vector3;
 private var tempX:float;
 private var tempY:float;
 private var tempZ:float;
  
 5.function Update () {
     if (Input.GetMouseButtonDown(0)) {
             tempX = Camera.main.ScreenToWorldPoint(Input.mousePosition.x);
             tempY = transform.position.z;
             tempZ = Camera.main.ScreenToWorldPoint(Input.mousePosition.y);
             target = new Vector3(tempX, tempY, tempZ);
         }
 10.        transform.position = Vector3.MoveTowards(transform.position, target, 10 * Time.deltaTime);
 }

(my apologies if this isn't accurate on line8. Im a C# user not a javascript user so I don't know if jacascript needs to use the word 'new' before the vector3).

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 lvl9hero · Jun 21, 2014 at 01:54 AM 0
Share

heya, thanks for trying to help me out, really thankful :)

but i'm already getting some errors, it seems like Vector3 can't be used as a "float"?

here's the errors Unity is giving:

 $$anonymous$$ovement.js(12,51): BCE0017: The best overload for the method 'UnityEngine.Camera.ScreenToWorldPoint(UnityEngine.Vector3)' is not compatible with the argument list '(float)'.
 $$anonymous$$ovement.js(12,51): BCE0022: Cannot convert 'UnityEngine.Vector3' to 'float'.

Speaking of my camera's angle, it is setup in a traditional 3rd-person angle, somewhat at a 45degree tilt downwards looking/chasing the player...

Will that really complicate movement with mouseclicks? I was imagining it was simple as making sure we're using World coordinate values ins$$anonymous$$d of local... or something like that yea.. hehe

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotate game object and then return to its original rotation 1 Answer

Problem with camera and character controller (fighting game) 1 Answer

Move player in a cyclic path? 0 Answers

Player movement 0 Answers

How can I get player to move when only touching an object 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