- Home /
change the front point or head of a GameObject
i hav a cube gameobject of which i need to change the front position or head or its tip.i use a transform .foward function on the object like this:-
transform.position += transform.forward * Time.deltaTime * speed;
the problem is that the cube moves along the middle of one of its sides(fig-1)and not along one of its corners (fig-2).please help.
Answer by ElDo · Jan 14, 2015 at 06:05 AM
you could either Change the direction where to move your cube, or Change the direction. What your code does is move the cube along it's Forward direction (Z-Axis) which Points to one of its sides. Now you could Change that Forward direction to Point somewhere else and Keep your move code like this:
transform.Forward=new Vector3(0,1,1);
or you Keep the directions of the cube (Forward, Up and Right) but Change your move code to whereever you want it to move to like that:
transform.Position+=new Vector3(0,1,1) * Time.deltaTime * Speed;
sorry dosent work because i only need the object to move from ONE specific corner and not all corners and adding your code makes it come to the origin
so you only want to move one Corner and leave the other Corners where they are and deform the cube or what? then you Need to get the Vertices first and move them respectively like:
//get the mesh data first
$$anonymous$$esh mesh = transform.GetComponent<$$anonymous$$eshFilter>().mesh;
//then get all the Vertices
Vector3[] Verts=mesh.vertices;
//now move your Vertice(s)
Verts[0]+=transform.Forward;
mesh.vertices=Verts;
this should do the trick
that just moves the vertice only where as simultaniously the cube must also move
sorry this only changes one vertice where as i want the cube to also follow the same vertice around
so then it's the very first answer just except that you wouldn't use new Vector3(0,1,1) you'd use new Vector3(1,1,1) for the Corner between UP,FORWARD and RIGHT. the code would be:
transform.position+=new Vector3(1,1,1)*0.02f*Time.deltaTime;
if you would want to move it along another Corner you just have to Change the Vector3 to (1,1,-1),(1,-1,1),(1,-1,-1),(-1,1,1),(-1,1,-1),(-1,-1,1) or (-1,-1,-1) depending on which Corner of the 8 this cube has you want to use towards.
Your answer
Follow this Question
Related Questions
Moving game objects 2 Answers
Sorting list of Transforms 1 Answer
Collision delete 1 Answer
GameObject not working with variable in C# script 3 Answers
Cube moving through rigid body 2 Answers