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 rggstech · Jun 22, 2014 at 08:57 PM · gameobject

Moving an object based on position of other object

Hi,

I am creating a Pong game where I have two bars and the movement of first bar (Player 1) is generated by the system whereas the user would control the other bar (Player 2). The goal of both the players is to hit the ball which is moving between the two bars.

I am writing the code to move Player 1 based on position and velocity of the ball. The code works fine and Player 1 in most cases reaches the expected position of the ball. But in some cases, I see Player 1 going through a very rapid zig-zag movement even though the expected position of the ball is not changing.

The following is my code to compute the position of Player 1. This code is inside the update method of the ball.In only compute the Y position, because on the X asis player 1 remains stationary. I only change position of player 1 if the ball is moving towards it (i.e. if rigidbody2D.velocity.x < 0)

if(rigidbody2D.velocity.x < 0){

var player1X:float = player1.position.x;

var player1Y:float = player1.position.y;

var midHeight:float = player1.lossyScale.y / 2;

var ballX:float = transform.position.x;

var ballY:float = transform.position.y;

var timeToMove:float = (player1X - ballX) / (rigidbody2D.velocity.x);

var expectedYBall:float = rigidbody2D.velocity.y * timeToMove + ballY;

if(player1Y + midHeight - 0.2 < expectedYBall){

player1.rigidbody2D.velocity.y = 10;

}

else if(player1Y - midHeight + 0.2 > expectedYBall){

player1.rigidbody2D.velocity.y = -10;

} else {

player1.rigidbody2D.velocity.y = 0;

}

}

Attached herewith is screenshot for your reference. Please let me know if I am missing something. Pong ScreenShot

pong question.png (9.8 kB)
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 BsseeJ · Jun 22, 2014 at 11:00 PM

I'm not entirely sure what's wrong because I'm new myself, but I'd suggest looking into these two functions.

This may work better than using velocity http://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html or http://docs.unity3d.com/ScriptReference/Vector2-up.html

I got the impression that you were saying the zigzag was on the x axis but I'm not sure, if that's so you can try this. http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html You can use mathf.clamp to clamp an axis in transform.

I wish I understood Java better so I could write up where I'd replace the code but maybe that documentation will help anyways. Good luck, hope I was of any help.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Scripts accessing one another (JS) 0 Answers

how can I display a variable as a GUIText 5 Answers

Checking if a position is occupied in 2D? 1 Answer

Why does "Optimize Game Objects" break my armature and animations? 1 Answer


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