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 drodrii · May 24, 2014 at 03:20 PM · transformlerpprojectilemovetowardsscreentoworldpoint

Creating Projectile - MoveTo/Lerp

Hello everyone,

so the Idea is that this script creates a projectile that is launched towards where the user clicked, but this ain't happening.

Here's what I've got:

 userInputPosition = (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
 
             Debug.Log("Step1: " + userInputPosition);
 
             //Transform that userInput into WorldMap Coordenates.
             userInputPosition = Camera.main.ScreenToWorldPoint(userInputPosition);
 
             Debug.Log("Step2: " + userInputPosition);
 
             //Change the 'z' to collide with objects.
             userInputPosition = (new Vector3(userInputPosition.x, userInputPosition.y, 0));
 
 
 //Once we get where the user clicked...
 projectileDestination = userInputPosition;
 
 transform.position = Vector3.MoveTowards(transform.position, projectileDestination, moveSpeed * Time.deltaTime);
             


Everything is working moreless ok, the point of origin is correct, the coordenates according to the logs are correct but the projectiles aren't moving towards where the user clicked.

Here's some Debug.Logs:

Step1: (515.3, 216.4, 0.0)

UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:40)

Step2: (-2.2, 1.0, -10.0)

UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:45)

User clicked here:(-2.2, 1.0, 0.0)

UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:53)

Here's The player:(-2.3, 1.1, 0.0)

UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:54) The prjoectiles move towards another spot other from where the user clicked...
Comment
Add comment · Show 13
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 robertbu · May 24, 2014 at 03:32 PM 0
Share

If the camera is perspective (not orthographic), then there is an issue with the position that you pass to ScreenToWorldPoint(). The 'z' needs to be the distance in front of the camera. If you pass 0 when using a perspective camera, ScreenToWorldPoint returns the camera position.

avatar image drodrii · May 24, 2014 at 04:19 PM 0
Share

@robertbu

Thanks for your time, as always.

So, The game is 2D. I left this part out. What I did is just get the same x & y values , took the player's position. (Which can be seen in the logs). As the camera is fixed to the player you might say the player is the center of the camera, which would make the pointoforigin as always. What I don't understand is why its giving me all the right numbers but the wrong actions...

I do this //Change the 'z' to collide with objects. userInputPosition = (new Vector3(userInputPosition.x, userInputPosition.y, 0));

because in know the camera is in z=10; and I don't want the origin of projectile to be on (x,y,10)

For example.

Player(0,0,0) UserClick(2,2,0)

What happens

PointOfOrigin - Correct PointOfDestination - (-1,2,0) or something, I haven't really figured out the pattern it does... Which is what I don't understand...am I using $$anonymous$$oveTowards/Lerp function correctly?

avatar image robertbu · May 24, 2014 at 04:32 PM 0
Share

So are you saying the camera is Orthographic? If so, you code looks fine. As a quick test, I did this:

 #pragma strict
 
 var moveSpeed = 3.0;
 
 function Update() {
     var userInputPosition = (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
  
      Debug.Log("Step1: " + userInputPosition);
 
      //Transform that userInput into World$$anonymous$$ap Coordenates.
      userInputPosition = Camera.main.ScreenToWorldPoint(userInputPosition);
 
      Debug.Log("Step2: " + userInputPosition);
 
      //Change the 'z' to collide with objects.
      userInputPosition = (new Vector3(userInputPosition.x, userInputPosition.y, 0));
  
  
     //Once we get where the user clicked...
     var projectileDestination = userInputPosition;
  
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, projectileDestination, moveSpeed * Time.deltaTime);
 }

And the object smooth followed the mouse position. Start a new 2D scene, put an object in the scene, add this script to the object and hit play. So if the camera is Orthographic, then the problem is highly likely to be elsewhere.

avatar image drodrii · May 24, 2014 at 04:43 PM 0
Share

@robertbu Its not orthographic, the camera is on top of the player looking down, $$anonymous$$ind of like pokemon and gameBoy's Zelda... Let me try this and I'll let you know

avatar image robertbu · May 24, 2014 at 05:12 PM 1
Share

$$anonymous$$y/your above code will not work for a perspective camera. For a perspective camera with a rotation of (0,0,0) try this:

 #pragma strict
 
 var moveSpeed = 3.0;
 
 function Update() {
 
     var dist = transform.position.z - Camera.main.transform.position.z;
     var userInputPosition = (new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist));
     
  
      Debug.Log("Step1: " + userInputPosition);
 
      //Transform that userInput into World$$anonymous$$ap Coordenates.
      userInputPosition = Camera.main.ScreenToWorldPoint(userInputPosition);
  
     //Once we get where the user clicked...
     var projectileDestination = userInputPosition;
  
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, projectileDestination, moveSpeed * Time.deltaTime);
 }

 
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

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

Related Questions

Smooth swapping my game object 0 Answers

Move Transform to Target in X seconds 3 Answers

smooth fps movements using joystick 1 Answer

Turning character slowly with Slerp 1 Answer

Leading Reticle 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