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 Guruguy69 · Mar 14, 2014 at 10:27 AM · c#

How do I freeze X-Axis movement?

In my Pong-Clone I am able to get my enemy paddle to follow the ball's movement on Y axis using a Vector3.MoveToward function but am unable to freeze it's movement toward the ball on the X axis. The freeze X position button in the rigidbody component doesn't work and this is probably due to the Vector3.MoveToward function being inside the void Update ().

Any suggestions?

 using UnityEngine;
 using System.Collections;
  
 public class EnemyMove2Ball : MonoBehaviour {
  
     public Transform target; // drag the target (Ball) here
     float speed = 5.0f; // move speed
  
     void Update()
     {
        transform.position = Vector3.MoveTowards
          (rigidbody.position, 
           target.position, 
           speed*Time.deltaTime);       
     }
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KhShani · Mar 14, 2014 at 01:33 PM

go to the rigidbody compnent of the gameobject and there u will find a property "constraints " just check the x axis. and your gameobject x axis movement would be freeze

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
avatar image
-1

Answer by Linus · Mar 14, 2014 at 10:48 AM

On line 14

 //JS
     transform.position.x = 0;
 //C#
 transform.position = new Vector3(0f, transform.position.y, transform.position.z);

Freeze position in the editor only applies to physics. But you don't use that, and probably should check isKinematic if you have not done that.

The example code is if the paddle is to be in a particular place on x axis. You could also store the x location before moving it, and change transform.position.x back to what it should be afterwards.

Comment
Add comment · Show 6 · 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 benni05 · Mar 14, 2014 at 10:50 AM 0
Share

Won't work. You cannot modify just the x factor of a transform.position. Try it yourself.

avatar image Linus · Mar 14, 2014 at 11:03 AM 1
Share

oops, it works in UnityScript but get error in c#

avatar image benni05 · Mar 14, 2014 at 11:04 AM 0
Share

Not in CSharp which he is using. You'll get an error message:

 Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

and have to assign the whole position.

avatar image benni05 · Mar 14, 2014 at 11:10 AM 1
Share

at Linus: You should switch to CSharp if you are working with Unity for years already :) Can't believe how to survive on JS for such a long time :) Ok, blame-mode-off :)

avatar image Linus · Mar 14, 2014 at 11:11 AM 1
Share

Updated answer. Thanks for pointing out, I could have sworn it would work the same in C#.

Show more comments
avatar image
-1

Answer by benni05 · Mar 14, 2014 at 10:49 AM

That's it. A non-kinematic Rigidbody should be moved by for example AddForce (and related) function. Then it will take your freeze-settings take into account. Also call this function preferably in the FixedUpdate() method. What you are doing is to directly manipulate the transform.position which is possible as well. In this case you could do the following in order to maintain the x-position.

 Vector3 newPosition = Vector3.Lerp(transform.position, target.position, speed*Time.deltaTime);
 transform.position = new Vector3(transform.position.x, newPosition.y, transform.position.z);

This way there would be no need for a Rigidboy and the z position is maintained as well.

Comment
Add comment · Show 4 · 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 Linus · Mar 14, 2014 at 10:52 AM 0
Share

He might need Rigidbody for collision and collider detection.

avatar image benni05 · Mar 14, 2014 at 10:52 AM 0
Share

That's why I explained both ways.

avatar image Guruguy69 · Mar 14, 2014 at 12:21 PM 0
Share

I think Benni's add force suggestion applies more to this game. I need to read up on Lerp as well. Currently I have rigid bodies on everything, with is kinematic turned off, and my collider methods are on the ball itself, which generates random forward motion from a range on both x and y.

I was able to get something that works well enough for me to finish up. But I got a question as well.

If I were to make another Pong project, and apply Benni's provided script, I wouldn't need rigidbodies for any objects right? What's the easiest way to make the ball move without collision detection?

avatar image benni05 · Mar 14, 2014 at 12:51 PM 0
Share

There is no need for Rigidbodies if you want exact(!) control over the movement and behaviour of your Transforms. Using the physics engine means you can relatively easy let your Transforms perform realistically looking movements, acceleration, bounces etc. But it comes at a price, for example collision detection problems for fast moving objects (going through wall etc.) and a general performance overhead (though only when it really gets complicated/crowded). In a 2D-Pong-like game I would prefer to have precise control because you do not want the ball enter slightly into the bat (could happen with Physics), you would like to bounce it off pixel-perfect and crisp. The movement would be acchieved by those Lerp and Slerp methods (or the $$anonymous$$oveTowards you have used which is basically a Lerp with an additional parameter).

avatar image
-1

Answer by Bluewell · Mar 14, 2014 at 11:10 AM

Try this in your Update() function, so it only moves along y and z axis and having a x axis position value of '0' for target Vector3;

 void Update()
         {
                 transform.position = Vector3.MoveTowards
                 (transform.position, 
                   new Vector3(0, target.position.y,target.position.z), 
                  speed*Time.deltaTime);       
         }
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

24 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

Related Questions

Learning scripting in C Sharp. 1 Answer

How to make a player health script and enemy damage script that is easily changed? 2 Answers

Different script for each item? 1 Answer

Adding door sound line in the C sharp script help. 2 Answers

Car horn sound looping problem C# 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