Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 kaiserium16 · Jun 25, 2019 at 12:22 AM · camerascript.vector3offset

How do you randomize Vector3 offset (x, y, z) values overtime in script?

Hi there!

I am new to script writing. I currently have a script that tracks a player:

using UnityEngine;

public class camera : MonoBehaviour {

 public Transform target;
 public float smoothSpeed = 0.125f;
 public Vector3 offset;

 void LateUpdate ()
 {
     Vector3 desiredPosition = target.position + offset;
     Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
     transform.position = smoothedPosition;

     transform.LookAt(target);
 }

}

I want the Vector3 offset values (x, y, z) to change every 10 seconds so that the camera re-manuevers itself around the player. Does anyone know how to change the Vector3 offset values in script? So for instance every 10 seconds the values of (x, y, z) randomly change between values of let's say -5 and 5? ie. (3, 2, -2) .... (-2, 1, 5).

Thank you

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
2

Answer by metalted · Jun 25, 2019 at 06:06 AM

So the solution comes in two parts, one for the timer and one for the actual RNG. I don't think there is a Vector3.Random yet so you will have to assign each axis separately. To create a random Vector3, you can use the following code:

 Vector3 randomVector = new Vector3(Random.Range(-5f, 5f), Random.Range(-5f, 5f), Random.Range(-5f, 5f)); 

You can of course assign it directly to the offset, or assign the randomVector to the offset, your choice.


For the timer part, there are many ways to do this. It think the best way for a beginner would be to just have a float as a time, and use Time.deltaTime to subtract 1 each second. Then after 10 seconds, randomize the vector and add 10 to the timer.


Then there is the coroutine. Within the coroutine you can make use of "yield return new WaitForSeconds()" to add a delay to your code. You could combine this with a while loop to assign a random vector3 each 10 seconds.


Also there is InvokeRepeating(), I'm not really familiar with this method so I'm not sure how well it will fit in your project, but I know it will keep calling a function for every x-seconds. So this is also a way to go.


I hope this will give you enough information to get you going.

Comment
Add comment · Show 1 · 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 Bunny83 · Jun 25, 2019 at 10:37 AM 0
Share

I would highly recommend to set a $$anonymous$$imum distance for the "randomVector" since otherwise you could end up inside the object. So it probably makes more sense to use Random.onUnitSphere to get a direction and scale it by another random value between a $$anonymous$$ and max value. If you really want to have a "box" volume (which doesn't make much sense since you would be biased to the corners of the box) you could simply clamp the magnitude to a $$anonymous$$ distance.

 if (randomVector.magnitude < $$anonymous$$Dist)
     randomVector = randomVector.normalized * $$anonymous$$Dist;


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

202 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 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

How do you change the values of Vector3 Offset (x, y, z) in script randomly/overtime? 0 Answers

C# - Offset In-Air Camera from Current Object Position 1 Answer

How to get the forward vector normal to the camera's forward vector regardless of camera pitch 1 Answer

How to rotate mouse addresses? 0 Answers

(Vector3.Lerp) Camera doesn't reach the specified Destination! 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