Lerp Problem

So the problem is that I am instantiating a ‘bullet’ gameObject and am attempting to make it move to a location based on mousePosition. I have debugged the mouse position after Camera.main.ScreenToWorldPoint() and the value is correct. I’ve used Time.deltaTime * 1.0f and Time.Time (as you can see below). Changing between the two time values does change what happens but neither end up lerping to the specified location. I’ve also changed the value I am multiplying by but nothing I have done creates even a lerping motion. I just have no idea why it isn’t working. Thank you for any help you give.

void Start ()
{
character = GameObject.Find(“Character”);
startTime = Time.time;
}

void Update () 
{
     if(Input.GetButtonDown("Fire1"))
     {
	   rayCastMouse =     Camera.main.ScreenPointToRay(Input.mousePosition);

       Vector3 mousePos = Input.mousePosition;
       mousePos.z = 2;

       Vector3 pos = Camera.main.ScreenToWorldPoint(mousePos);

      Physics.Raycast(rayCastMouse, out rayHit, 10.0f);

      float distCovered = (Time.time - startTime) * 1.0f;
      float journeyLength = Vector3.Distance(new Vector3(0,1,0), pos);
      float fracJourney = distCovered/journeyLength;

	 if(rayHit.collider == null)
	 {	
	    tempBullet = GameObject.Instantiate(bullet,new Vector3(0,1,0), Quaternion.identity) as GameObject;
				
	    tempBullet.transform.position = Vector3.Lerp(tempBullet.transform.position, pos,fracJourney);
	  }
	else if(rayHit.collider.tag != "UI")
	{
	 tempBullet = GameObject.Instantiate(bullet,new Vector3(0,0,0), Quaternion.identity) as GameObject;
	tempBullet.transform.position = Vector3.Lerp(tempBullet.transform.position,pos, fracJourney);
	}
}
	
}

A lerp function should be used directly in an update function, if you call a lerp function once, it will not meet its endpoint, so each lerp function call is one step closer to its endpoint.

When I use a function to enable a certain movement, I set a boolean to true, and in the update function, the lerp method will be repeated as long as the boolean is true.

bool moveBullet = false;

void Update () 
{

     if(moveBullet)
     {
        tempBullet.transform.position = Vector3.Lerp(tempBullet.transform.position,pos,fracJourney);
     }


     if(Input.GetButtonDown("Fire1"))
     {
       rayCastMouse =     Camera.main.ScreenPointToRay(Input.mousePosition);
 
       Vector3 mousePos = Input.mousePosition;
       mousePos.z = 2;
 
       Vector3 pos = Camera.main.ScreenToWorldPoint(mousePos);
 
      Physics.Raycast(rayCastMouse, out rayHit, 10.0f);
 
      float distCovered = (Time.time - startTime) * 1.0f;
      float journeyLength = Vector3.Distance(new Vector3(0,1,0), pos);
      float fracJourney = distCovered/journeyLength;
 
     if(rayHit.collider == null)
     { 
        tempBullet = GameObject.Instantiate(bullet,new Vector3(0,1,0), Quaternion.identity) as GameObject;
 
        tempBullet.transform.position = Vector3.Lerp(tempBullet.transform.position, pos,fracJourney);
      }
    else if(rayHit.collider.tag != "UI")
    {
     tempBullet = GameObject.Instantiate(bullet,new Vector3(0,0,0), Quaternion.identity) as GameObject;
     moveBullet = true;
    }
}
 
}

Didn’t check for compile errors, but if you get what I mean, you won’t need to copy this :slight_smile: