Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tayyab43 · Nov 08, 2013 at 04:48 PM · c#transformlerp

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);
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roojerry · Nov 08, 2013 at 04:51 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);    
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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); 
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tayyab43 · Nov 08, 2013 at 06:32 PM 0
Share

lol, your right but new vector3 also comes up with an error that says you can't use lerp with a transforms?

avatar image zombience · Nov 08, 2013 at 07:11 PM 0
Share

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);
avatar image tanoshimi · Nov 08, 2013 at 07:20 PM 0
Share

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);
     }
     }
 }
avatar image tayyab43 · Nov 09, 2013 at 02:37 PM 0
Share

why is my background black ?

avatar image tayyab43 · Nov 09, 2013 at 02:37 PM 0
Share

it used to be blue?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does object lerp to above the object it is lerping to? 1 Answer

C# - Mathf.Lerp: NullReferenceException: Object reference not set 1 Answer

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers

Shrinking an Object 1 Answer

Move position with smoothing 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges