The question is answered, right answer was accepted
what wrong with lerp.
using UnityEngine;
using System.Collections;
public class Mousedrag : MonoBehaviour {
// Use this for initialization
public Transform player;
public float speed;
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0))
{
Debug.Log(Input.mousePosition);
Vector3 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float yPos = newPos.y;
player.position=new Vector3(player.position.x,yPos,player.position.z);
player.position = Vector3.Lerp(player.position, yPos, speed);
}
}
}
You aren't incrementing the speed variable. Well not that I can see anyway. It also appears you are using Lerp incorrectly. Look at this website, which will guide you in the right direction. http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly
They don't necessarily need to change the speed variable.
That is true, however, it's not the correct way to do it. It will constantly be lerping the position at whatever the value of "speed" is. When really, it should be a value that lerps between 0 and 1. 0% of the distance to 100% of the distance.
Does this code compile? You're passing a float as the second parameter to Vector3.Lerp, when it should be a Vector3. Looks to me like it should be newPos
ins$$anonymous$$d of` yPos`. That would make player
's position move towards newPos
ie move towards the mouse pointer, on the x/z axes (you've already snapped its position to the same as newPos on the y axis).
I'm already guessing, though, since you tell us neither what the code's supposed to do nor what's going wrong.
Answer by Le-Pampelmuse · Nov 23, 2015 at 02:54 PM
Hi. You have to be specific when asking a question on this forum.
Don't just post your script and ask people to correct it.
This is not how Unity Answers works.
Please read the Frequently Asked Questions and the User Guide.
This will help you to ask good questions later. :)
Your code has some errors that prevent Unity from even starting the scene: Vector3.Lerp()
takes (Vector3, Vector3, float)
and not (Vector3, float, float)
.
You declared yPos
as a float
.
If you look at the error in Unity, it will tell you that. :)
Have a nice day.
Follow this Question
Related Questions
Interpolated movement in a circle 1 Answer
Setting slider value over time using C# script? 0 Answers
Using lerp properly 2 Answers
Mathf.Lerp crashing build? 1 Answer
Vector3.lerp for moving transform 0 Answers