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 godoy · Oct 29, 2012 at 10:52 PM · forceadd

AddForce(C#) not works

tenho um prefab que estou tentando instanciá-lo em um script e aplicar a ele uma força inicial de lançamento, consigo instanciar mas não consigo aplicar esta força, meu código:

 public class MissileLauncherC : MonoBehaviour 
 {
     public  Rigidbody Projectile;
     public float speed = 20;
     void Start()
     void  Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Rigidbody teste;
             teste = Instantiate(Projectile, transform.position, transform.rotation) as Rigidbody;
             teste.rigidbody.AddForce(transform.forward * speed * Time.deltaTime, ForceMode.Impulse);            
             Physics.IgnoreCollision(Projectile.collider, collider);
         }
     }
 }

alguém pode me ajudar?

I have a prefab that I'm trying to instantiate it in a script and apply a force to it initial launch, I can instantiate but can not apply this force.

can anyone help me?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Oct 29, 2012 at 10:56 PM

Time.deltaTime is only needed when you apply a continuous force every frame. An impuls is a one time thing, so remove the Time.deltaTime. Also keep in mind that ForceMode.Impulse takes the mass of the object into account. The greater the mass the smaller the effect. You might want to use ForceMode.VelocityChange instead or check your speed / mass ratio.

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 Bunny83 · Oct 29, 2012 at 10:59 PM 0
Share

btw, it looks like your Start method is missing the body... $$anonymous$$aybe just a copy & paste error or it got lost during the formatting here.

avatar image
0

Answer by godoy · Oct 31, 2012 at 12:08 PM

i still can not, my script: clone.AddForce = Vector3.forward * Speed;

the object is instantiated but not hurled i also tried: clone.velocity = Vector3.forward Speed; clone.rigidbody.AddForce(clone.transform.forward speed); and others bud did not work :(

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
0

Answer by godoy · Nov 01, 2012 at 05:52 AM

consegui agora :)

o código completo:

using UnityEngine; using System.Collections;

public class MissileLauncherC : MonoBehaviour

{ public Rigidbody Projectile;

public Transform Launcher;

public float Speed = 20;

void Start() { }

void Update () { if (Input.GetButtonDown("Fire1")) { Rigidbody clone; clone = Instantiate(Projectile, Launcher.position, Launcher.rotation) as Rigidbody; clone.velocity = transform.forward * Speed; } } }

sou novo por aqui mas estarei a disposição para ajudar sempre quando puder!

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
0

Answer by CollosalChris · Mar 20, 2014 at 09:46 PM

First thing to check if AddForce doesn't work is to make sure your object as a RigidBody and does not have 'isKinematic' turned on

If those things are good then another common mistake is to try to addforce to a GameObject you just initiated. If that is the case then don't forget to set your initiated object to a gameobject that you can then alter (don't miss the casting 'as GameObject'!!!)

 public GameObject PREFAB;
 public Vector3 THE_OBJECT_SPAWN_TRANSFORM;

 // Use this for initialization
 void Start () {
     StartCoroutine (CreateNewObjectToMove());
 }
 
 IEnumerator CreateNewObjectToMove()
 {
     yield return new WaitForSeconds (2);

     Quaternion OBJECT_ROTATION = Quaternion.identity;
     GameObject OBJECT_I_CAN_CHANGE;

     OBJECT_I_CAN_CHANGE = Instantiate (PREFAB, THE_OBJECT_SPAWN_TRANSFORM, OBJECT_ROTATION) as GameObject;
     OBJECT_I_CAN_CHANGE.rigidbody.AddForce(PREFAB.transform.forward * 200);
     
 }
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
0

Answer by etcarew · Mar 03, 2016 at 03:14 PM

public class PlayerControls : MonoBehaviour { public GameObject ball; public int numbBallAllowed; public Transform servePoint; private GameObject[] numbOfBalls; public float ballForce = 1000f; void Start () {

 }
 
 void Update ()
 {
     numbOfBalls = GameObject.FindGameObjectsWithTag("Ball");
     if (Input.GetKeyDown(KeyCode.R) && numbOfBalls.Length +1 <= numbBallAllowed)
     {
         GameObject balls;
         balls = Instantiate(ball, servePoint.position, servePoint.rotation) as GameObject;
         balls.rigidbody.AddForce(ball.transform.forward * ballForce);
     }
 

 }

} // doesnt work, anyone know why?

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

12 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

Related Questions

Apply force then slowly back off 4 Answers

Why can't Animation run functions with Rigidbody.AddForce? 1 Answer

Bullet add force to ragdoll help 0 Answers

Add Force: Increase Speed of Ball periodically 3 Answers

Precalculate force before applying it. 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