Answers for "how to make a object move in unity"

C#
7

how to make an object move in unity

Vector3 input = new Vector3 (Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical");
Vector3 dir = input.normalized;
Vecotr3 vel = dir * speed * Time.deltaTime;

transform.Translate(vel);
Posted by: Guest on March-02-2020
4

unity how to move an object

float x = 10f; //the x value for the object's position
float y = 10f; //the y value for the object's position
float z = 10f; //the z value for the object's position
Vector3 position = new Vector3(x, y, z); // this will create a vector with the values of the x, y, and z values
transform.position = position; //this change the object that this script is attached to position to the position vector that was created
Posted by: Guest on December-19-2019
0

Move Object Unity

//README!!!
//THIS ONLY MOVES UP AND DOWN IN A LOOP FOR THE Y AXIS.
//Just apply this script to a gameobject adjust it to your needs.
 
 
	//The x, y, z speed of your object
    public Vector3 Movement_Speed;

    //When the game starts, will it be moving up or down?
    public bool IsMovingDown = true;

	//How long will it be moving up and down?
    //Ex: 3f = 3 seconds of it moving up, then it will move down for 3 seconds
    public float TimeToSwitchFromUpAndDown = 3f;

	//A Fixed Update is a Updated that is called every fixed frame
    void FixedUpdate()
    {
        //Just moves the object based on your speed you inputed
        gameObject.transform.Translate(Movement_Speed * Time.fixedDeltaTime);
    }


    void Start()
    {
        //If you changed the y speed of the object then...
        if (Movement_Speed.y > 0)
        	//start the method UpAndDown() that moves the object on the y axis in a loop
            StartCoroutine(UpAndDown());
    }


    public IEnumerator UpAndDown()
    {
    	//Change this while loop condition that will keep it running for as long as you need it
        while (FindObjectOfType<PlayerController>().IsAlive == true)
        {
            //If it will move down then..
            if (IsMovingDown == true)
            {
            	//make it the inputed y value negative
                Movement_Speed.y = -Movement_Speed.y;
            }
			
            //If it will move up then...
            else if (IsMovingDown == false)
            {
                //make the inputed y value positive
                Movement_Speed.y = 3;
            }
			
            //Look at TimeToSwitchFromUpAndDown varible at top of script
            yield return new WaitForSeconds(TimeToSwitchFromUpAndDown);
	
            //If it was moving down then...
            if (Movement_Speed.y < 0)
            {
           	//make MovingDown false so it moves up when the while() loops 
                IsMovingDown = false;
            }
			//If it was moving up then...
            else if (Movement_Speed.y > 0)
            {
            //make MovingDown true so it moves up when while() loops
                IsMovingDown = true;
            }
        }
    }
//Hopefully there ain't any bugs!
}
Posted by: Guest on November-16-2021

Code answers related to "how to make a object move in unity"

C# Answers by Framework

Browse Popular Code Answers by Language