- Home /
restrict Lerp to only one axis?
good morning; when i try to compile this in VS:
transform.localScale.x = Vector3.Lerp(posA, inputPosB, fracJourney);
it gives me error cs1612, basically what i need to do is scaling an object along an axis non istantly. I also need to translate it to make it look still (bc there is no shear in unity). this is the rest of the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lerper : MonoBehaviour
{
[SerializeField] Vector3 inputPosB = Vector3.zero;
[SerializeField] Vector3 result;
[SerializeField] Vector3 avoidBehindCamera;
private GameObject thisCube;
private float lengthC;
[SerializeField] float growFactor = 10f;
bool isCoroutineRunning = false;
bool noPosB = true;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
PosB();
noPosB = false;
}
void PosB()
{
if (Input.GetMouseButtonUp(0))
if (isCoroutineRunning == false)
{
inputPosB = Camera.main.ScreenToWorldPoint(Input.mousePosition + avoidBehindCamera);
Vector3 posA = this.transform.position;
Vector3 journey = (inputPosB - posA);
float journeyLength = Vector3.Magnitude(journey);
float journeyAngle = Vector3.Angle(journey, Vector3.right);
if (transform.position.y > inputPosB.y) { journeyAngle = 0 - journeyAngle; }
Debug.Log(" : inputPosB" + inputPosB + " : journeyLength " + journeyLength + " : journeyAngle " + journeyAngle);
float startTime = Time.time;
StartCoroutine(Scale(startTime,journeyLength,posA,inputPosB));
}
}
private IEnumerator Scale(float startTime,float journeyLength,Vector3 posA,Vector3 inputPosB)
{
isCoroutineRunning = true;
Debug.Log(isCoroutineRunning);
Debug.Log(journeyLength);
Debug.Log(transform.localScale.x);
while (journeyLength > transform.localScale.x )
{
float distCovered = (Time.time - startTime) * growFactor;
// Debug.Log(distCovered);
float fracJourney = distCovered / journeyLength;
transform.localScale.x = Vector3.Lerp(posA, inputPosB, fracJourney);
Debug.Log(transform.localScale.x);
yield return null;
}
isCoroutineRunning = false;
Debug.Log(isCoroutineRunning);
}
}
if restricting lerp is not possible, is there a better way to do this?
Answer by Bunny83 · Apr 17, 2019 at 01:21 PM
Sorry but it's completely unclear what you want to do. You seem you try to use the position value of your object and the position of your mouse click somehow as scale for your object. This doesn't make much sense at all. Scale is a unitless factor which is usually "1" while positions are given in world units.
Apart from that localScale is a property of a value type. In order to change just one component of it you have to do it in 3 steps
read the whole vector3 into a local variable
change the component(s) you want in the local variable
assign the local variable back to the property
Though currently you try to assign a Vector3 (which is what Vector3.Lerp returns) to a float value which of course doesn't work.
If you want to scale an object to fit between two points you need several things setup correctly. First of all the origin of the object should be located at the start position. Next you have to know the size of your object at scale 1. Preferably having an object that has already a size of 1 unit at a scale of 1. I've posted an example over here how to rotate and scale a cube (which has it's origin shifted already) so it fits between two points.
When your scale actually matches world units you can directly use your "distCovered" value as scale for your object. Something like this:
transform.localScale = new Vector3(distCovered, 1, 1);
There are way too many unknowns in your post to give a clear answer.
actually I rewrote everything by following the example you linked and it worked :D Yes actually it wasn't very clear because the object gets instatiated at a position given by another script. I posted another question about that other script (involvingcolliders and rays, completely theoretical no code involved,) i would be very happy if you could check it :) BTW thanks a lot for the effort in understanding what i wanted and thanks again for the example given :D