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
1
Question by brendans95 · Jun 13, 2015 at 09:39 AM · texturematerialoffset

Offset texture ONCE on key press

Hiya all! I've been searching for the answer to this for like an hour now so I figured I'd just ask myself haha

I'm trying to make a texture (512x256) that has 8 different faces on it offset. The model's head is UV'd to one facial expression and when you offset the texture/UVs by 0.25 on X the UVs perfectly fit the next expression (and 0.5 on Y).

alt text

I'm not exactly a programmer and the only script I can find that will offset just pans through it constantly. This is the code I've grabbed from http://docs.unity3d.com/ScriptReference/Material-mainTextureOffset.html:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public float scrollSpeed = 0.5F;
     public Renderer rend;
     void Start() {
         rend = GetComponent<Renderer>();
     }
     void Update() {
         float offset = Time.time * scrollSpeed;
         rend.material.mainTextureOffset = new Vector2(offset, 0);
     }
 }

I've seen that you could animate the UVs within Unity's animator but I'd prefer 2 float values (X and Y) that I could change within the inspector. It's probably super easy considering I just want values in the Material inspector to change. If we can just get it to go up on the X by 0.25 on [KeyPressA] and go up by 0.5 on the Y on [KeyPressB] I should be fine to adapt that to other inputs.

If anyone can direct me to what I need to remove from this code I will be super grateful!

rstr0002-00-fi-face.png (14.6 kB)
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
1
Best Answer

Answer by zach-r-d · Jun 13, 2015 at 10:53 AM

You want to use the following Input.GetKeyDown in your Update() method, like this:

 if (Input.GetKeyDown("Q")) {
     rend.material.mainTextureOffset += new Vector2(/* x offset */);
 }
 if (Input.GetKeyDown("W")) {
     rend.material.mainTextureOffset += new Vector2(/* y offset */);
 }

Input.GetKeyDown only returns true when the key has gone from up to down on that frame (compared to Input.GetKey, which is true every frame until the key is up again).

Comment
Add comment · Show 4 · 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 brendans95 · Jun 14, 2015 at 12:02 AM 0
Share

Thanks for your response Zach!

So this is definitely the Update() method I needed just help me out one bit more and let me know what I'm whacking in the

 += new Vector2(/* x offset */);

part? It doesn't seem to like me using

 += new Vector2(0.25);

and that's obviously because we're not stating we want a new value in the X alone. Using

 (0.25,0); 

didn't work either... what am I doing wrong here hahaha

avatar image zach-r-d · Jun 14, 2015 at 12:48 AM 0
Share

For the x offset, you would pass (.25f, 0f), and for y offset, you would pass (0f, .5f)

avatar image brendans95 · Jun 14, 2015 at 12:51 AM 1
Share

Oh my god you did it I love you man hahaha - thanks so much! Works like a charm ^^

avatar image zach-r-d · Jun 14, 2015 at 12:54 AM 1
Share

Great, glad it works!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Texture offset not behaving like it should (?) 1 Answer

Offset detail texture in c# 1 Answer

Scroll Ball Texture Smoothly 0 Answers

Why can't I animate texture offset? 1 Answer

Texture / Material offset doesn't work 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