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 Thorn Blackstar · Jan 15, 2012 at 12:35 AM · movementforcestuck

Movement reacting when

Im trying to have an object move back and forth, ofc when it hits something i need it to start moving the other direction, which is where I have a problem, im trying to create a stuck boolean if its X pos is the same as its last X pos, so when its "stuck" it will move the other way, if someone could tell me what in my code is wrong, ive a feeling its the if statements but not sure

var forwardspeed : float; var backwardspeed : float; var Currentpos : float; var Stuck : boolean; var forwardmoving : boolean;

function Update () { if(transform.position.x == Currentpos) { Stuck=true; } else { Stuck=false; }

 if(Stuck == false)
 {
     if(forwardmoving == true)
     {
     rigidbody.AddForce(Vector3(forwardspeed,0,0));
     }
     if(forwardmoving == false)
     {
     rigidbody.AddForce(Vector3(backwardspeed,0,0));
     }
 }
 
 else if(Stuck == true)
 {
     if(forwardmoving == true)
     {
     rigidbody.AddForce(Vector3(backwardspeed,0,0));
     forwardmoving = false;
     Stuck = false;
     }
     if(forwardmoving == false)
     {
     rigidbody.AddForce(Vector3(forwardspeed,0,0));
     forwardmoving = true;
     Stuck = false;
     }
 }

}

function LateUpdate () { Currentpos = transform.position.x; }

Comment
Add comment · Show 2
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 adrenak · Jan 15, 2012 at 11:19 AM 0
Share

What is the problem you get in the game?

avatar image Thorn Blackstar · Jan 15, 2012 at 12:37 PM 0
Share

the gameobject with the script keeps walking into an object and stuck does not trigger as been on, also if I set to true it doesn't move the other way

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by adrenak · Jan 15, 2012 at 06:37 PM

I dont think that the above approach is the best one. You will probably never get the variable stuck to change because the object is continously applying force and it rebounds. What you see is not it keeps on walking into the object, its rebounding and then undergoing a forward force again and again rapidly over very small distances and looks like its at the same spot.

You can use transform.Translate as it is not time dependent but I doubt that even that will work.

But if I were you, I would go for raycast, it works 100%. You will have to have two rays, one at the front, one at the back, and the only change in the logic will be that you will have to change the direction of motion when the point where the ray collides with the object is very near, like 0.05 units of something, that means it has almost collided and it turns back. The advantage of this method is that it can be expanded to do many other functions later if you want and is much more hassle free. Remember you dont always have to check for physical collisions, because physical collisions are detected only 50 to 60 times per second ( that is your frame rate BTW).

I Hope this helps! If you want to do it the raycast way, then you can check out the scripting manual itself, it has all the things you need for this and is written in a clear manner.

Comment
Add comment · Show 1 · 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 Thorn Blackstar · Jan 15, 2012 at 08:23 PM 0
Share

cool ty, ill look into raycasting the object ins$$anonymous$$d

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

Simple 2-Button-Mash script 1 Answer

Directional rolling a ball ( upwards / left only ) 0 Answers

Tried unsuccesfully many tips, vectorial force will keep adding to my object 1 Answer

Limiting Speed on Two Axis 1 Answer

Sprite staying on idle animation 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