• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Have a character face their moving direction? 1 Answer

Player Controller moves automatically forward. cant stop. 1 Answer

My Player is crossing the ground through air 1 Answer

How to Logically Match Ground Slope While Using This Code? 1 Answer

Move player in a cyclic path? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges