• 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 kyon77 · Apr 14, 2012 at 07:42 AM · movementrigidbodyaddforcespaceship

RigidBody movement with AddForce

So I´ve been working on a spaces$$anonymous$$p controls for a few days and I got a decent script, but I ran into a problem and I dont know how to fix it.

The game is kinda like All Range Mode in Starfox 64, you can move freely in X, Y an Z and all that stuff. The problem is that when a bullet$$anonymous$$ts my RigidBody s$$anonymous$$eld sometimes collides, some other not =(

I read some docs and it says that using transform to move a RigidBody it´s not the best choice so I tried to use AddForce... and I failed =D

I end up moving too fast, or not moving at all, either way I couldn´t control the s$$anonymous$$p

so if anyone can help me turn t$$anonymous$$s transform movement script into a AddForce movement script I would really aprecciate it.

 void Movimiento()
 {
     //Velocidad
     if(gameObject.rigidbody.velocity.magnitude < VelNaveMax ) //Compara si la velocidad es menor a a la velocidad maxima, si es verdadero...
     {
         gameObject.rigidbody.velocity = transform.forward * VelNave; //mueve la nave hacia adelante en una cantidad definida por la vel del motor
     }
 
     //Vuelta
     if(Input.mousePosition.x < (Screen.width / 2) + 70) //Si el mouse se encuentra a la izquierda
     {
         transform.Rotate (0,-Rotacion,0); //rota la nave una cantidad negativa definida en la variable rotacion
     }
      
     if(Input.mousePosition.x > (Screen.width / 2) - 70) //Si el mouse se encuentra a la derecha
     {
         transform.Rotate (0, Rotacion,0); //rota la nave una cantidad definida en la variable rotacion
     }
     
     //Subir/bajar
     if(Input.mousePosition.y < (Screen.height / 2) + 70) //Si el mouse se encuentra abajo
     {
         transform.Rotate (Rotacion,0,0); //Rota hacia abajo la nave
     }
     
     if(Input.mousePosition.y > (Screen.height / 2) - 70) //Si el mouse se encuantra arriba
     {
         transform.Rotate (-Rotacion,0, 0); //rota el objeto hacia arriba
     }    
     
     //Girar
     if(Input.GetAxisRaw ("Horizontal") > 0){ //Mientras se presione "A"
         transform.Rotate (0,0, -Rotacion); //Gira la nave a la derecha
     }
     
     if(Input.GetAxisRaw ("Horizontal") < 0){ //Mientras se presione "D"
         transform.Rotate (0,0, Rotacion); //Gira la nave a la izquierda
     }
     
     //Acelerar
     if(Input.GetAxisRaw ("Vertical") > 0 && VelNave <= VelNaveMax){ //Mientras se presione "W"
         if((VelNave + 10) < VelNaveMax  && Acelerando == false){ //Si el aumento de velocidad no supera la velocidad maxima
             VelNave = VelNave + 10; //Aumenta la velocidad de la nave en 10
             
             Acelerando = true;
             StartCoroutine (Reset ());
         }
     }
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 Tseng · Apr 14, 2012 at 12:50 PM 1
Share

Please, keep variable names and comments in English, otherwise no-one can understand what it means. This is a general guideline for clean code, since English is the international language and everyone should be able to understand it.

avatar image kyon77 · Apr 14, 2012 at 08:39 PM 0
Share

Ok I´ll remember that thanks

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by yezzer · Apr 14, 2012 at 07:53 AM

A tip: For t$$anonymous$$s kind of behaviour, you'll need to apply it over several frames, as it's a continuous force.

http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html

"If you want to apply a force over several frames you should apply it inside FixedUpdate instead of Update"

You'll also either want to use ForceMode.Force or ForceMode.Acceleration.

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 kyon77 · Apr 14, 2012 at 08:40 PM 0
Share

Yeah void Movimiento() is called from Fixed Update, but thanks n_n

avatar image
0

Answer by fafase · Apr 14, 2012 at 08:13 AM

I t$$anonymous$$nk you are dealing with frame space problem. If the projectile $$anonymous$$tting your s$$anonymous$$p is moving real fast, it might happen that not collision occur.

ex: your bullet moves at 500m/s, for 50fps (to simplify) you have 10/frame so:

at t:0 it is at 0

at t:1 it is at 10

at t:2 it is at 20 and so on

So if you are at 15 and less than 5m wide, there will be no collision.

That is why you should use raycast to check collision at $$anonymous$$gh velocity and then the actual projectile is just there for the visual effect.

You can also try to use continuous collision detection. T$$anonymous$$s is from the rigidbody documentation:

Continuous Collision Detection

Continuous collision detection is a feature to prevent fast-moving colliders from passing each other. T$$anonymous$$s may happen when using normal (Discrete) collision detection, when an object is one side of a collider in one frame, and already passed the collider in the next frame. To solve t$$anonymous$$s, you can enable continuous collision detection on the rigidbody of the fast-moving object. Set the collision detection mode to Continuous to prevent the rigidbody from passing through any static (ie, non-rigidbody) MeshColliders. Set it to Continuous Dynamic to also prevent the rigidbody from passing through any other supported rigidbodies with collision detection mode set to Continuous or Continuous Dynamic. Continuous collision detection is supported for Box-, Sphere- and CapsuleColliders.

Now if t$$anonymous$$s does not do the trick the way you want, you should use raycast. That will send an invisible ray (invisible in the game) in front of the object and report if the ray collides with anyt$$anonymous$$ng. Just like the blind man cane.

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

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 kyon77 · Apr 14, 2012 at 08:42 PM 0
Share

Thanks, that´s a good option, I´ll try it

avatar image
0

Answer by ttzilla · Apr 14, 2012 at 12:49 PM

I t$$anonymous$$nk for fixing the collision problem you should be able to change the collision detection mode to continous or continous dynamic. That way it should chek more frequently. Check t$$anonymous$$s out: Rigidbody

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 kyon77 · Apr 14, 2012 at 08:41 PM 0
Share

I did that, but I still have the same problem

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

RigidBody.AddForce not working 1 Answer

How to make an object move in an arch 1 Answer

How to move Character with AddForce 1 Answer

What is the best way to move the player with physics without using MovePosition? 0 Answers

Rotate object towards target without influencing AddForce 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