I’m not sure what type a variable is and what to save it as.I’v saved it as a Transform but it comes up with an error, heres my code:
using UnityEngine;
using System.Collections;
public class TheProperAimDownSight : MonoBehaviour{
Transform positionA =new Transform(0.3946629f,-0.3439038f,1.099834f);
Transform positionB =new Transform(0.009479392f,-0.3292553f,1.099835f);
bool lerp;
void Update (){
if(Input.GetMouseButtonDown(0))
{
lerp = true;
}
if(Input.GetMouseButtonUp(0))
{
lerp = false;
}
if(lerp)
transform.position = Vector3.Lerp(transform.localPosition, positionB, Time.deltaTime);
else{
transform.position = Vector3.Lerp(transform.localPosition, positionA, Time.deltaTime);
}
}
You can’t instantiate a Transform without a GameObject. Transforms are just a way of storing coordinates and rotations, and they come attached to GameObjects. If you need new Transforms that do not refer to preexisting transforms, you need to create a GameObject for each new transform.
try this:
Transform positionA;
void Start()
{
positionA = new GameObject().transform;
positionA.position = new Vector3(.01f, .03f, .05f);
}
Also, if you’re just trying to store coordinates, you want to store those values in a Vector3.
Like so:
Vector3 PositionA = new Vector3(.0001f, .0002f, .0003f);
Vector3 PositionB = new Vector3(.0002f, -.0002f, -.0005f);
float speed = .5f;
void Update()
{
if(lerp)
transform.position = Vector3.Lerp(PositionA, PositionB, speed);
}
“it comes up with an error” is not the most descriptive way of asking for help, but I’m guessing that the error in question is complaining about the following lines:
Transform positionA =new Transform(0.3946629f,-0.3439038f,1.099834f);
Transform positionB =new Transform(0.009479392f,-0.3292553f,1.099835f);
Transform is a component, but you’re trying to construct it with three floating point values (which is what the position element of a transform would look like). Looking at the rest of your code, it looks like what you meant to write was
Vector3 positionA =new Vector3(0.3946629f,-0.3439038f,1.099834f);
Vector3 positionB =new Vector3(0.009479392f,-0.3292553f,1.099835f);