How do I move an object in C#?
I am new to using Unity to script, and I just started C# (and it looks similar to C++ so far).
Anyways, I tried to use this to find the objects position and change it.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript: MonoBehaviour
{
void Start (){
Transform.Position.X = 2;
Debug.Log("Test");
}
}
Instead I get an error, so how do I change size, position, and such?
Thanks.
Considering this is the bare basics of unity scripting, I would suggest doing more research or learning a bit more before you ask a question. This could easily be found in a beginners tutorial :p
@pickle chips Considering this is a forum for questions and answers and that this person had asked a question, by logic you should answer ins$$anonymous$$d of making a comment about how he should be going elsewhere to get said answer. That is all.
Agreed. I'm also a beginner, but all the tutorials I can find for beginners are video-based, and I'm not good at learning stuff like this from videos -- I much prefer written information -- so questions like this are extremely useful to me.
Answer by jacobschellenberg · Aug 01, 2013 at 02:21 AM
using UnityEngine; using System.Collections;
public class NewBehaviourScript: MonoBehaviour
{
public float movementSpeed = 10;
void Update(){
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
}
transform is the current object this script is attached to. Translate calls the Translate properties. Vector3.right (might be capital Right), tells the object to move on the X axis on the positive direction. You multiply this by movementSpeed to increase the units moved, and lastly by Time.DeltaTime to ensure the object is moving at a time based speed rather then by how fast your processor can move it.'
You can also do, Vector3.up, right, left, down, forward, back. For size you would do, transform.localScale = new Vector3(10,10,10);
This is really basic, but you'll get it :)
Hope this helps :)
: Operator -' cannot be applied to operands of type
UnityEngine.Vector3' and `int'
How to choose where to move it to?