Domanda

Hi everybody I am having a problem with my game, the problem is based when I switch the materials so it can face the other direction (it is a 2d game). This affects the animation which is controlled by the renderer.material.mainTextureOffset but I have no idea why this is not working.

code in c#:

using UnityEngine;
using System.Collections;

public class character : MonoBehaviour {
    public float forwardSpeed = 10.0f;
    public Material newMaterialRefcs1;
    public Material newMaterialRefcs2;
    // Use this for initialization
    void Start () {

    }
    //error is occurring here !!!!!!!! below Important. The 2 if statements below
    // Update is called once per frame
    void Update () {
    if( Input.GetKey(KeyCode.D)){
            renderer.material.mainTextureOffset = new Vector2(0.25f, 0);
            transform.position += -transform.right * forwardSpeed * Time.deltaTime;
                renderer.material = newMaterialRefcs1;

    }if( Input.GetKey(KeyCode.A)){
            renderer.material.mainTextureOffset = new Vector2(0.25f, 0);
            transform.position += transform.right * forwardSpeed * Time.deltaTime;
                renderer.material = newMaterialRefcs2;

    }

}}
È stato utile?

Soluzione 3

I have figured it out with some help from the unity forums took a while but here it is:

using UnityEngine;
using System.Collections;

public class character : MonoBehaviour {
    public float forwardSpeed = 20.0f;  public float rot = 0f;public float jumpSpeed = 100;public float gravity = 30f;
    public Material newMaterialRefcs1;
    public Material newMaterialRefcs2;

    void Start () {

    }
    public float scrollSpeed = 0.25F;
     void Update () {
    if( Input.GetKey(KeyCode.RightArrow)){
            scrollSpeed += 0.25f;
            transform.position += -transform.right * forwardSpeed * Time.deltaTime;
            renderer.material = newMaterialRefcs1;
            float offset = scrollSpeed;
        renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
    }if( Input.GetKey(KeyCode.LeftArrow)){
            scrollSpeed += 0.25f;
            transform.position += transform.right * forwardSpeed * Time.deltaTime;
            renderer.material = newMaterialRefcs2;
            float offset = scrollSpeed;
        renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));

    }
        Vector3 isgrounded = transform.TransformDirection(Vector3.up);
        if( Input.GetKeyDown(KeyCode.Space)&& Physics.Raycast(transform.position, isgrounded, 6)){
            transform.position -= transform.up * jumpSpeed * Time.deltaTime*2;
} 
         Physics.gravity = new Vector3(0, gravity, 0);
        transform.rotation = Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z);
}
}

Altri suggerimenti

I'm not sure exactly what your problem is but it sounds like you're trying to adjust one setting in the first script then add a new material in the second script. If that is the case then, when you assign the new material in the second script you are replacing the material completely meaning the renderer.material.mainTextureOffset.x that you set in the first gets overwritten.

Try keeping the offset in a separate variable that you can assign to the new texture when it is added in the second script.

Try to use other shader. Diffuse, for example.

Also, check in inspector, is this value really changed. May You rewrite it in other script?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top