Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Sellari · Feb 05, 2016 at 05:14 AM · scripting problem

Moving object alone y-axis

I have look at other posts but I'm still confused as to how to do it I want to move these bricks in a breakout game from position 3 to 2 and back and forth, starting from y-position 3. I have never learned C# before and I'm just building with codes I got different places. Here are my codes:

using UnityEngine; using System.Collections;

public class MoveBricks : MonoBehaviour {

 void Update()
 {
     var vel = GetComponent<Rigidbody2D>().velocity;
     var pos = GetComponent<Rigidbody2D>().position;
     GetComponent<Rigidbody2D>().position = pos;
     if (pos.y >= 3)
     {
         vel.y = -.5f;
         GetComponent<Rigidbody2D>().velocity = vel;
     }
     if (pos.y >= 2f && pos.y <= 3)
     {
         GetComponent<Rigidbody2D>().velocity = vel;
     }

     if (pos.y <= 2f)
     {
         vel.y = .5f;
         GetComponent<Rigidbody2D>().velocity = vel;
     }
   
 }

}

These codes move my brick from 3 to around 4.6 then to 3.6 and back and forth from there. Why?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Feb 05, 2016 at 07:42 AM

The reason is you're using Rigidbodys and their velocities. They are physics objects and have other forces acting on them like inertia and drag. Rather than the 'go here, stop, and do this' kind of mentality of a typical coding process they have the more physically-based properties of 'for every action there is an equal and opposite reaction'. The bottom line for your purposes is that, assuming you don't want to wrestle with the physics, you want to manipulate the GameObject's transform directly instead. Something like this:

 transform.position += Vector2.right * Time.deltaTime;

Putting that in the update loop will move your block at one unit per second to the right. This kind of position manipulation is a lot easier than trying to fly around like a spaceship in my opinion.

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 Sellari · Feb 07, 2016 at 07:40 PM 0
Share

I don't fully understand how that line is suppose to work. All it did was move my bricks to (0, 0) and move it to the whatever Vector.2(x, y) and back once then it starts twitching. Sometimes it will move it have way then go back to (0, 0). Is this because it is updating too fast or that one second time frame is too short? Even if it's not, how can can change the time frame? It also wouldn't let me use += on Vectors

avatar image Sellari · Feb 07, 2016 at 07:45 PM 0
Share

Also How can I have it so it doesn't start at (0, 0) ? Should I change the positions using Start and then transform? Or can I set transform.position equal to a position?

@cjdev

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

43 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED]My OnMuseDown execute only once 1 Answer

First person shooter : Leaning! 2 Answers

NavMesh of an automatically generated maze 0 Answers

How can I fix this script or make one similar? 1 Answer

Menu object not responding 0 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