How can I make an object transform to given position and rotation
For example, if I want to move my object to x=10, y=5, z=2 position and rotate to x=45, y=0, z=45 rotation, how will I be able to do this using the following code transform.Translate(1f * Time.deltaTime, 0f, 0f);
for position and using this code
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
for rotation.
Translate will move the object by that amount, not to those coordinates. Same with Rotate.
Use:
transform.position = new Vector3(10,5,2);
transform.rotation = Quaternion.Euler(new Vector3(45,0,45));
(going by memory alone, not 100% sure on .Euler function, perhaps it's ".FromEuler" or something. Let Intellisense help you.
I already tried this, it works. I am using it for camera so I want a smooth movement. The player who is playing my game must see a smooth change in camera motion not a sudden change. But thanks for answering because the code you posted is shorter than my code. so I just want to see the movement of the camera while reaching the given position.
Oh I'm sorry, I missed this.
If that is the case, may I suggest looking to linear interpolation functions? https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
https://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
The interpolation factor moves between 0.0f and 1.0f - simply move that via your update loop to have your camera transition from one place to another slowly over some time.
Answer by SimonClintonIv · Jun 07, 2016 at 02:41 PM
using UnityEngine;
using System.Collections;
public class CamTrack : MonoBehaviour {
public GameObject Player;
public GameObject Camera;
public static int global; //global variable
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void LateUpdate()
{
//Default position
if (Camera.transform.position.x < Player.transform.position.x) //move right
{
transform.Translate(1f * Time.deltaTime, 0f, 0f);
}
if (Camera.transform.position.x > Player.transform.position.x) //move left
{
transform.Translate(-1f * Time.deltaTime, 0f, 0f);
}
if (Camera.transform.position.z < Player.transform.position.z + -3) //move front
{
transform.Translate(0f, 0f, 1f * Time.deltaTime);
}
if (Camera.transform.position.z > Player.transform.position.z + -3) //move back
{
transform.Translate(0f, 0f, -1f * Time.deltaTime);
}
if (Camera.transform.position.y < Player.transform.position.y + 1) //move up
{
transform.Translate(0f, 1f * Time.deltaTime, 0f);
}
if (Camera.transform.position.y > Player.transform.position.y + 1) //move down
{
transform.Translate(0f, -1f * Time.deltaTime, 0f);
}
}
}
This is what I wanted
Answer by $$anonymous$$ · Jun 04, 2016 at 05:11 PM
to rotate:
transformObject.eulerAngles = Vector3(x,y,z) //x,y,z or Vector3 variable
to move:
transformObject.translate(x,y,z); //x,y,z or Vector3 variable
@Eudaimonium has correct answer too :) but i prefer translate :D