x


translate a gameobject along the X-axis.

Hi All,

I'm trying out some of my first ever scripting by creating a simple scene where I am trying to move a cylinder along the X-Axis. I have written the following but whenever I attempt to move the object via the right or left arrow keys instead of moving directly along the X-Axis my object appears to also rotate slightly around the Z-axis.

Can anyone give me a pointer or advise what it is I'm doing wrong? Thanks, code listing of my script which is attached to a cylinder with a rigidbody that is placed on top of a plane:

 var speed = 0.5;

    function Update () {

        var horiz : float = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        transform.Translate(horiz,0,0);
     }
more ▼

asked Jun 19 '11 at 08:15 PM

Rob212 gravatar image

Rob212
16 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Oops, typically I've just answered my own question. I had a rigid Body on the cylinder and didn't notice there was an attribute on the it named FreezeRotation. I ticked this and it solved my problem.

more ▼

answered Jun 19 '11 at 08:26 PM

Rob212 gravatar image

Rob212
16 1 1 1

IF you plan to control the cylinder via script you should turn the Rigidbody into a kinematic Rigidbody (check isKinematic).
http://unity3d.com/support/documentation/ScriptReference/Rigidbody-isKinematic.html

Also don't forget to mark an answer as the right answer so the question is marked as solved.

Jun 19 '11 at 09:02 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x293
x206
x29

asked: Jun 19 '11 at 08:15 PM

Seen: 1329 times

Last Updated: Jul 16 '11 at 09:35 PM