- Home /
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:
UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:40)Step1: (515.3, 216.4, 0.0)
UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:45)Step2: (-2.2, 1.0, -10.0)
UnityEngine.Debug:Log(Object) clickShootScript:Update() (at Assets/Scripts/FirstLevelScripts/Weapon/clickShootScript.cs:53)User clicked here:(-2.2, 1.0, 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...Here's The player:(-2.3, 1.1, 0.0)
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.
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?
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.
@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
$$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);
}
Your answer
Follow this Question
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