i tried it like this but it gives an error i don’t know what to do with…I’m just beginning with C#
my goal is to take it’s current position and translate an added amount over y axis on Input.GetKey…
errors:
Assets/HEAVEN_ASSETS/playOrgan.cs(39,34): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected *
Assets/HEAVEN_ASSETS/playOrgan.cs(45,51): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer **
public float move;
public float smooth = 5.0F;
Vector3 target = Vector3(transform.position.x, transform.position.y +move, transform.position.z) ; [COLOR="red"]*[/COLOR]
while (i < mesh.vertices.Length) {
if(Input.GetKey(key)){
transform.Translate = Vector3.Lerp( transform.position, target, Time.time *smooth;[COLOR="blue"]**[/COLOR]
...
}
Why are you looping through every vertex in mesh.verticies? That input.getKey probably doesn’t belong there, unless I’m badly mistaking what you’re trying to do?
If you’re trying to transform the object along Y, wouldn’t you just do this?
Transform myTransform = transform;
Vector3 target = myTransform.position;
target.y += move;
float step = .1f; // or whatever you want the step to be…
myTransform.position = Vector3.Lerp(myTransform.position, target, step);
You could stick that in a statement checking whether Input.GetKey(key) is true. Can you clarify what you’re doing with the while() loop?
yes sorry should have taken that out, I’m also lerping vertex colors on the same key.
it’s for a puzzle where i want the player to play on a organ.
I want to move colored blocks to a path of heights when playing the right sequence of notes.
trying to keep the script as compact as possible, i want to be able to determine the note, the color, the height and the key for groups of same color blocks in the inspector.
here’s the whole script:
edit thanks man your little piece help out great
here’s my code now, only thing i can’t get to work is getting it back to it default position, i hoped the getkeyup would fix it, but that is setting it to it’s default - move. although if i say target.y=target.y; it doesn’t move probably because it’s new value stays target + move…
using UnityEngine;
using System.Collections;
public class playOrgan : MonoBehaviour {
public KeyCode key;
public AudioClip sound;
public int transpose;
public int note;
public Color defaultColor;
public Color newColor;
public float speed;
public float move;
public float smooth;
// Use this for initialization
void Start () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Color[] colors = new Color[mesh.vertices.Length];
int i = 0;
while (i < mesh.vertices.Length) {
colors[i] = defaultColor;
i++;
}
mesh.colors = colors;
}
// Update is called once per frame
void Update () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Color[] colors = mesh.colors;
int i = 0;
Vector3 target = transform.position;
while (i < mesh.vertices.Length) {
if(Input.GetKey(key)){
colors[i] = Color.Lerp(colors[i], newColor, speed * Time.deltaTime);
}else {
colors[i] = Color.Lerp(colors[i], defaultColor, (speed * Time.deltaTime)/2);
}
i++;
}
mesh.colors = colors;
if(Input.GetKey(KeyCode.LeftShift)){
transpose = 4;
}else{
transpose = -8;
}
if(Input.GetKeyDown(key)){
target.y = target.y + move;
transform.position = Vector3.Lerp( transform.position, target, (Time.time *smooth));
if (note>=0){
audio.pitch = Mathf.Pow(2, (note+transpose)/12.0F);
audio.PlayOneShot(sound);
}
}
if(Input.GetKeyUp(key)){
target.y = target.y - move;
transform.position = Vector3.Lerp( transform.position, target, (Time.time *smooth));
}
}
}
using UnityEngine;
using System.Collections;
public class playOrgan : MonoBehaviour {
public KeyCode key;
public AudioClip sound;
public int transpose;
public int note;
public Color defaultColor;
public Color newColor;
public float speed;
public float move;
public float smooth;
// Use this for initialization
void Start () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Color[] colors = new Color[mesh.vertices.Length];
int i = 0;
while (i < mesh.vertices.Length) {
colors[i] = defaultColor;
i++;
}
mesh.colors = colors;
}
// Update is called once per frame
void Update () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Color[] colors = mesh.colors;
int i = 0;
Vector3 target = transform.TransformPoint(0,move,0);
while (i < mesh.vertices.Length) {
if(Input.GetKey(key)){
transform.position = Vector3.Lerp( transform.position, target, Time.time *smooth);
colors[i] = Color.Lerp(colors[i], newColor, speed * Time.deltaTime);
}else {
colors[i] = Color.Lerp(colors[i], defaultColor, (speed * Time.deltaTime)/2);
}
i++;
}
mesh.colors = colors;
if(Input.GetKey(KeyCode.LeftShift)){
transpose = 4;
}else{
transpose = -8;
}
if(Input.GetKeyDown(key)){
if (note>=0){
audio.pitch = Mathf.Pow(2, (note+transpose)/12.0F);
audio.PlayOneShot(sound);
}
}
}
}
transform.Translate is a method, not a variable, you can’t assign a value to it.