0
\$\begingroup\$

Hi i just wanna move my object smoothly from one position to another but object disappear and immediately teleport to position where i clicking anyone know whats happen ?

    if (Input.GetKeyDown(KeyCode.Mouse0))
    {

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, 1000.0f))
            {

                Vector3 newpos = new Vector3(hit.point.x, 0, hit.point.z);
                Cube40.transform.position = Vector3.Lerp (Cube40.transform.position, newpos, 1f);

            }
    }

Thank you Vishnu i already fix it with code below your code was better anyway but there is another problem with Lerp method itself because no matter where i click object always transform his position with the same time and it can't be use in RTS game rather i need something like transform.Translate with from:Vector3, to:Vector3 parameters.

GameObject Cube40;
RaycastHit hit;
Vector3 newPosition;

void Start()
{

    Cube40 = GameObject.Find("Cube1");
    newPosition = Cube40.transform.position;

}

void Update()
{

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit, 1000.0f))
    {

        Vector3 newpos = new Vector3(hit.point.x, 0, hit.point.z);
        if(Input.GetKeyDown (KeyCode.Mouse0))       
        newPosition = newpos;
        Cube40.transform.position = Vector3.Lerp (Cube40.transform.position, newPosition, 0.1f);

    }               
}   
}
\$\endgroup\$
  • 3
    \$\begingroup\$ If I remember correctly, Lerp will interpolate between the two points based on the 3rd parameter which should be a percentage distance between the two points. msdn.microsoft.com/en-us/library/… \$\endgroup\$ – UnderscoreZero Jul 8 '13 at 16:26
  • \$\begingroup\$ I know that but why i have that kind of problem when i use it with raycasting ? \$\endgroup\$ – user23899 Jul 8 '13 at 16:44
  • \$\begingroup\$ Have you tried using values other than 1.0f for the third parameter? Have you tried animating the parameter over time? \$\endgroup\$ – Exilyth Jul 8 '13 at 17:11
  • 1
    \$\begingroup\$ I do not believe your problem is in your raycasting since you say the object appears where you've clicked. Your problem is that you are transforming your cube's position directly to the new position by using 1f in your lerp function. \$\endgroup\$ – UnderscoreZero Jul 8 '13 at 17:14
  • \$\begingroup\$ lerp is looking for a value between 0f and 1.0f for the third paramater. Anything greater an 1 will be interpreted as 1, and anything less than 0 will be interpreted as 0. You have the value as a flat 1f, so of course it will teleport (100% of the second parameter). \$\endgroup\$ – Kirbinator Jul 8 '13 at 18:41
2
\$\begingroup\$

Change these lines:

Vector3 newpos = new Vector3(hit.point.x, 0, hit.point.z);
Cube40.transform.position = Vector3.Lerp (Cube40.transform.position, newpos, 1f);

to something more akin to this

//initialize outside of the loop
float newPos = 0;

float rate = 1.0f; //change to what you want, Percent-covered/sec
Vector3 endpos = new Vector3(hit.point.x, 0, hit.point.z);
newPos += Time.deltaTime * rate;
Cube40.transform.position = Vector3.Lerp (Cube40.transform.position, endPos, newPos);

The rate of change in rate will cause the cube40 object to move toward the Mouse's raycast point. However, the quick and dirty way I have it now will cause it to accelerate (my thought experiment suggest it will decelerate) since the start point changes. You will want to make the start point constant as well if you want to avoid that.

*changed answer so percentage accumulates

\$\endgroup\$
  • \$\begingroup\$ Thanks for answers yes i tried with Time.deltaTime and few other combinations with Time class but when i click for example on x=800,y=0,z=1200 and Cube start position is x=300,0,1200 object moving only for 1-3 points to the position where i clicking \$\endgroup\$ – user23899 Jul 8 '13 at 20:23
  • \$\begingroup\$ The percentage should accumulate, this should work better. \$\endgroup\$ – Kirbinator Jul 8 '13 at 21:48
1
\$\begingroup\$

If you want your game to run smoothly independently of the platform it runs on, make sure you multiply your last value with Time.deltaTime. In slower platforms (e.g. mobile), the Time.deltaTime will be higher and the frames drawn will be lower so the lerp speed will remain exactly the same.

For example:

Cube40.transform.position = Vector3.Lerp (Cube40.transform.position, newpos, Time.deltaTime*0.5f);

\$\endgroup\$
0
\$\begingroup\$

I think your problem would be solved if you place your teleporting code outside of the Input segment if (Input.GetKeyDown(KeyCode.Mouse0))

Set a boolean variable keyFired to true when input is detected and place lerping code inside update and turn it false when the lerp completes. Try this, it worked for me.(assign your cube via game inspector to the gameobject variable Cube40 before running)

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{

    public GameObject Cube40;
    bool fireKey = false;
    Vector3 newpos;
    float tParam ;
    // Use this for initialization
    void Start () 
    {
        tParam = 0.0f;
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                fireKey = true;
                newpos = new Vector3(hit.point.x, 0, hit.point.z);

            }
        }
        if(fireKey)
        {
            if(tParam < 1.0)
            {
                float speed = 1.0f;//if you want , vary this
                tParam += Time.deltaTime * speed;
                Cube40.transform.position = Vector3.Lerp (Cube40.transform.position, newpos, tParam);   
            }
            else if(tParam >= 1.0)
            {
                tParam = 0.0f;
                fireKey = false;    
            }
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ The problem is that the third parameter puts it at the target position. third parameter goes between 0 and 1 \$\endgroup\$ – Sidar Jul 10 '13 at 3:09
  • \$\begingroup\$ I changed, the coding and tested it too, pls see above code. Thankyou. \$\endgroup\$ – Vishnu Jul 11 '13 at 4:02
  • \$\begingroup\$ @Vishnu check out update to my question \$\endgroup\$ – user23899 Jul 13 '13 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.