Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am new to unity and i was wanting to know how i can change a objects position and angles and stuff like that in a c# script. I exported the object from blender as .3ds. The objects name is sphere.3ds. please show and explain code.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

A quick and easy way to do this would be just to pass in the object as a variable.

Creating a public GameObject variable in a script will allow an object to be handed to it from the Unity interface.

public GameObject testObject;

Then when you go back into unity you can see a new box appeared in the script module. You can then drag the object you want to manipulate into that box that says "None (Game Object)" and it'll be passed into your script as that variable.

enter image description here

To then manipulate that object's position, you can just use its transform and do as you please. It's the object's transform variable which determines its position, rotation, etc.

Vector3 objectPos = testObject.transform.position;
Quaternion rotation = testObject.transform.rotation;

Also, the transform variable already supplies you with useful functions that you can mess around with, like Translate and RotateAround.

To rotate an object around an axis, use

testObject.transform.RotateAround(testObject.transform.position, Vector3.up, 90)

The first parameter is the position of the rotation, the second is the axis you want to rotate around, say up, left, right, etc.. And the third is the amount you want to rotate it by.

share|improve this answer
    
I added the public gameobject code but i don't see that box. – Ineedhelp Sep 7 at 18:49
    
Have you added the script to any object? And in addition, you can't name your GameObject variable object – Ashigaru Sep 7 at 18:50
    
Ok i got it thanks!! – Ineedhelp Sep 7 at 18:51
    
One more thing using the Quaternion code how would i rotate the object 90 degrees? – Ineedhelp Sep 7 at 18:52
    
To do that, I would use testObject.transform.RotateAround(testObject.transform.position, Vector3.up, angle) The angle parameter is just how much you want it to rotate, so in your case: 90. And the Vector3.up is the axis you want to rotate it around. In this example. Vector3.up will rotate it around its y axis. Vector3.right will rotate it around its right-pointing axis, etc. – Ashigaru Sep 7 at 18:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.