Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bazsee · May 02, 2012 at 10:27 AM · object movement

Object moving but not only on the axis i want

If i touch a button, it executes this function:

 function move_y(howmuch : int){
 if(howmuch > 0 && object.transform.localPosition.y<10)
 object.transform.Translate(Vector3.up, Space.World);
 if(howmuch < 0 && object.transform.localPosition.y>0)
 object.transform.Translate(Vector3.down, Space.World);
 }

The problem is, that the object is not moving only on the Y axis, but on X and Z axis too and slightly rotates on the Y axis. What can be the problem?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image venkspower · May 02, 2012 at 10:49 AM 0
Share

I think, the above script is making no sense at all. Since you are defining the variable "howmuch" in the function. But you never initialized it. And you are giving it in the condition at the same time!

avatar image venkspower · May 02, 2012 at 10:50 AM 0
Share

Or is the variable "howmuch" relevant to the position?

avatar image Seth-Bergman · May 02, 2012 at 10:56 AM 0
Share

howmuch is passed in on the function call:

move_y(2); //example

no need for an int here, but it's obviously a learning attempt

avatar image venkspower · May 02, 2012 at 11:04 AM 0
Share

Of course, I know that. But what is the purpose of it? It is nothing but hard-coding! If he passes a positive number, only one condition will be executed.

That is why I asked, is the variable "howmuch" related to position?

avatar image venkspower · May 02, 2012 at 11:06 AM 0
Share

It is working fine for both object.transform.localPosition.y and object.transform.position.y.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Seth-Bergman · May 02, 2012 at 10:44 AM

A few issues I see off the bat:

you probably don't want localPosition, try object.transform.position.y

if you just want to go straight up and down, you could say:

object.transform.position.y += howMuch;

if you want smooth movement though, better to use something like:

object.transform.position.y = Mathf.Lerp(object.transform.position.y, howMuch);

this would of course need to be called continuously, so maybe get that part into the update function.

maybe could be that physics are affecting the object, if it has a rigidbody

edit:

the easiest way I can think of to accomplish what you seem to want:

 function Update()
 {
 if(Input.GetKey("Fire1") && object.transform.localPosition.y<10)
 object.transform.position.y = Mathf.Lerp(object.transform.position.y, 10,Time.deltaTime);
 if(Input.GetKey("Fire2") && object.transform.localPosition.y>0)
 object.transform.position.y = Mathf.Lerp(object.transform.position.y, 0,Time.deltaTime);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bazsee · May 02, 2012 at 11:29 AM 0
Share

i've deleted the rigidbody from the object (it was set not to use gravity and it was kinematic, so i don't know what was the problem) and this way it seems like it is working now with the "object.transform.position.y += how$$anonymous$$uch" method.

avatar image Seth-Bergman · May 02, 2012 at 11:42 AM 0
Share

if it was set to is$$anonymous$$inematic it wouldn't have mattered, I was just throwing out a possibility.. At any rate, is that what you were after (with what you have)?

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

When attempting to place an object from prefab, the object continually jumps up to the camera and back down quickly. 0 Answers

Slerps and / or other pendulum-like movements 0 Answers

How to make a rigid body move along a specific path using my mouse? 1 Answer

Unity 5 - Can't move/control object 1 Answer

Hi. I'm a new Unity user, and I'm trying to return an object to its initial position after moving it. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges