Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Qinect · May 04, 2016 at 09:26 AM · c#lerprandom.rangevectors

Script is not getting a reference.

I am trying to make a script that would randomly lerp around an object. Here it is:


 using UnityEngine;
 using System.Collections;
 
 public class pingpongtest : MonoBehaviour 
 {
     void Start()
     {
         float x = Random.Range(-5f,5f);
         float y = Random.Range(-5f,5f);
         Vector3 newPos = new Vector3(x,y,0);
     }
     void Update()
     {
         transform.position = Vector3.Lerp(transform.position,newPos,Time.time);
         InvokeRepeating("Start",3,3);
     }
    
 }
 

Problem is that newPos doesn't know that I've set it as a Vector3 in the start function. What can I do to make this work? I've been trying alot of work arounds but none seem to work properly.


EDIT: for some reason my script is glitching so here is a text verison of it in case it glitches again:

using UnityEngine; using System.Collections;

public class pingpongtest : MonoBehaviour { void Start() { float x = Random.Range(-5f,5f); float y = Random.Range(-5f,5f); Vector3 newPos = new Vector3(x,y,0); } void Update() { transform.position = Vector3.Lerp(transform.position,newPos,Time.time); InvokeRepeating("Start",3,3); }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xesin · May 04, 2016 at 09:39 AM

First of all. You are trying to call InvokeRepeating inside the Update, this means that this starts a InvokeRepeating each frame. Second thing, you are declaring newPos inside the Start but trying to use this in the Update.

Posible solution:

 using UnityEngine;
 
 public class pingpongtest : MonoBehaviour
 {
     void Start() {
         InvokeRepeating("UpdatePosition", 3, 3);
     }
 
     void UpdatePosition() {
         float x = Random.Range(-5f, 5f); 
         float y = Random.Range(-5f, 5f); 
         Vector3 newPos = new Vector3(x, y, 0);
         transform.position = Vector3.Lerp(transform.position, newPos, Time.time);
     }
     
 }

Comment
Add comment · Show 3 · 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 Qinect · May 04, 2016 at 06:16 PM 0
Share

Well, it seems to be working except that the object is not lerping but teleporting.

avatar image Xesin · May 05, 2016 at 11:48 AM 0
Share

I did just that your script work. The problem is that your script does not do what you intend to do. Do you know how Lerp function works?

http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

avatar image Qinect Xesin · May 05, 2016 at 12:04 PM 0
Share

Thank you for your help! I found this: https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/ and it helped me understand lerp.

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

152 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random.Range kepps returing the same value 1 Answer

Instantiating random prefabs gives wrong position 3 Answers

Assign role randomly from array for my online game 2 Answers

continously spanws and overlaps 0 Answers

changing rpm when shifting 1 Answer


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