Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 matioma · Jul 13, 2016 at 05:06 PM · programming

RigidBody.It doesn't give speed to my gameObject

This script in my opinion should've given some volocity to my gameobject onEnable, however he just appears befeore the camera and fall down.Can anyone help me with that problem?

 using UnityEngine;
 using System.Collections;
 
 public class BulletProperties : MonoBehaviour 
 {
     public float BulletSpeed = 10000;
     void OnEnabled()
     {
         gameObject.rigidbody.velocity = new Vector3 (0, 0, BulletSpeed);
     }
     void OnCollisionEnter(Collision col)
     {
         this.gameObject.SetActive (false);
     }
 }

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 Glurth · Jul 13, 2016 at 07:25 PM 0
Share

If the object is affected by gravity, it will fall. If the falling is fast enough, relative to the forward speed, you might not notice that it IS moving forward. You can turn off "use gravity" for the rigid body, to test.

avatar image matioma Glurth · Jul 14, 2016 at 06:13 AM 0
Share

I've tried but it didn't help.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bodaciouslycrazy · Jul 13, 2016 at 08:47 PM

try using the Start() function instead of OnEnabled. Start is always called when a new object is instantiated, but OnEnabled is not called, because the "enable" property wasn't changed. It was always enabled.

Try this!

 public float BulletSpeed = 10000;
      void Start()
      {
          gameObject.rigidbody.velocity = new Vector3 (0, 0, BulletSpeed);
      }
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 matioma · Jul 14, 2016 at 06:12 AM 0
Share

The problem is I am using (object pooling);Which is done in other script, Everything works fine except the movement of the object. As I understood OnEnabled() is similar to Start().The difference is that Start() is called once when the object is created and OnEnabled()is called everytime gameObject is SetActive(true).

avatar image
0

Answer by matioma · Jul 14, 2016 at 08:55 AM

Thanks guys for help.I found the solution myself.Actualy I don't know why it didn't work that way but I copied

gameObject.rigidbody.velocity = new Vector3 (0, 0, BulletSpeed);

In this script

'public GameObject bulletType; public int numberOFBullets;

 public float timeBetweenShots;

 private float timeBetweenShotsRef;

 private List<GameObject> objectPoolingList = new List<GameObject>(); 

 void Start () 
 {
     timeBetweenShotsRef = timeBetweenShots;
     for (int i=0; i < numberOFBullets; i++) 
     {
         GameObject obj = (GameObject)Instantiate(bulletType);
         obj.SetActive(false);
         objectPoolingList.Add(obj);
     }
 }

 void Update () 
 {
     timeBetweenShotsRef -= Time.deltaTime;
     Debug.Log (timeBetweenShotsRef);
     if (Input.GetMouseButtonDown (0) && timeBetweenShotsRef < 0f) 
     {
         BulletSetActive();
         timeBetweenShotsRef = timeBetweenShots;
     }
 }

 void BulletSetActive()
 {
     for (int i=0; i< objectPoolingList.Count; i++) 
     {
         if(!objectPoolingList[i].activeInHierarchy)
         {
         objectPoolingList[i].transform.position = transform.position;
         objectPoolingList[i].transform.rotation = transform.rotation;
         objectPoolingList[i].SetActive(true);
            objectPoolingList[i].gameObject.rigidbody.velocity = new Vector3 (0, 0, 10);   // pasted code
             break;
         }
     }
 }

And It worked.

If someone knows the reason why it doesn't work the way I've written first time will be happy to get the answer.

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 _Yash_ · Jul 14, 2016 at 12:55 PM

It was a typo.

you wrote void OnEnabled()
it should be void OnEnable()

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Make something arrive at position in exactly X seconds? 2 Answers

Changing Enemy Prefab Variable (MoveSpeed) at Runtime 1 Answer

Programming custom objects instead of using GameObjects 0 Answers

Find a game object using the value of a variable in the script attached to it, or with it's transform.position? 2 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