- Home /
 
I'm not sure what type a variable is and what to save it as.
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);
     }
 }
 
              Did you check the parameters that Vector3.Lerp takes?(Lerp(Vector3 from, Vector3 to, float t);) You are giving it a Transform as the second parameter when it expects a Vector3.
Answer by zombience · Nov 08, 2013 at 04:54 PM
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);    
 }
 
              Answer by tanoshimi · Nov 08, 2013 at 05:01 PM
"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); 
 
              lol, your right but new vector3 also comes up with an error that says you can't use lerp with a transforms?
in your code above, you have written:
  transform.position = Vector3.Lerp(transform.localPosition, positionA, Time.deltaTime);
 
                  but you can't do that, because Vector3.Lerp needs to lerp from a Vector3 TO another Vector3.
so, in code, if I wrote
Debug.Log(transform.localposition));
I would get the Vector3 coordinates representing the transform's position.
If I wrote
Debug.Log(transform);
I would get the name of the gameObject that the transform is attached to.
In your code, you are attempting to Lerp from transform.localPosition (which is a legitimate Vector3 location) as the first argument, to PositionA, which is just a transform.
you need to ins$$anonymous$$d lerp from transform.localPosition TO PositionA.position.
So:
  transform.localPosition = Vector3.Lerp(transform.localPosition, positionA.position, Time.deltaTime);
 
                 Huh? Your code looks like this, right? Works for me...
     using UnityEngine;
     using System.Collections;
      
     public class TheProperAimDownSight : $$anonymous$$onoBehaviour{
     Vector3 positionA =new Vector3(0.3946629f,-0.3439038f,1.099834f);
     Vector3 positionB =new Vector3(0.009479392f,-0.3292553f,1.099835f);
     bool lerp;
      
     void Update (){
      
     if(Input.Get$$anonymous$$ouseButtonDown(0))
     {
     lerp = true;
     }
     if(Input.Get$$anonymous$$ouseButtonUp(0))
     {
     lerp = false;
     }
      
     if(lerp)
      
     transform.position = Vector3.Lerp(transform.localPosition, positionB, Time.deltaTime);
      
      
     else{
      
     transform.position = Vector3.Lerp(transform.localPosition, positionA, Time.deltaTime);
     }
     }
 }
 
                 Your answer