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 Frogsama · Jan 30, 2015 at 05:51 PM · rigidbodyobject move

Why is my object not moving

Hey Guys, i have a problem with my code I want an object to shoot bullets. Right now it spawns them at the right location but they dont move unless i bump into them with the player. Kinematic is off and the bullets have mass so thats not it. Also it doesn't matter if I use transform, velocity or AddForce. Nothing seems to work. Appreciate the help

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     public Object ballPrefab;
     public float speed;
 
     void Update ()
     {
         float length;
         float dist;
         Plane plane = new Plane(Vector3.up,0);
         Vector3 point = new Vector3 (0, 0, 0);
         Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
         if (plane.Raycast(ray, out dist))
         {
             point = ray.GetPoint(dist);
             
             length = Mathf.Sqrt((point.x - this.transform.position.x)*(point.x - this.transform.position.x)+ (point.y - this.transform.position.y)*(point.y - this.transform.position.y)+ (point.z - this.transform.position.z)*(point.z - this.transform.position.z));
             point.x = (point.x - this.transform.position.x)/length/1.5f;
             point.y = (point.y - this.transform.position.y)/length/1.5f;
             point.z = (point.z - this.transform.position.z)/length/1.5f;
             
         }
         if (Input.GetMouseButtonDown(0)){
             Rigidbody bullit = (Rigidbody)Instantiate(ballPrefab,this.transform.position + point, Quaternion.identity);
             bullit.rigidbody.AddForce(Vector3.forward*200);
         }
 
     }
     //before performing any physics calculations
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
 
         rigidbody.drag = 2;
         rigidbody.AddForce(movement * speed * Time.deltaTime);
 
 
 
         
 
     }
 
 }

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 rob5300 · Jan 30, 2015 at 06:25 PM 0
Share

It could be due to the force being added in update, ins$$anonymous$$d of FixedUpdate. You can try to make it run there ins$$anonymous$$d and see if that helps. Or you can have a script on the bullets that gives them an initial velocity when they are created?

Also try just adding the Vector3.Foward*200 and adding it to the objects velocity ins$$anonymous$$d.

avatar image Frogsama · Jan 30, 2015 at 10:49 PM 0
Share

Hey, yea i tried Velocity already. Also FixedUpdate. If i put it in FixedUpdate a click sometimes spawns two bullets, so i did not bother too much with it. I'm sure there is a sleep function or smth. there to prevent it. but yeah FixedUpdate and/or using velocity didnt change anything :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by InvincibleCat · Jan 30, 2015 at 07:00 PM

Seriously, before asking any question, look at the console!

First of all your prefab as to be a GAMEOBJECT and not a simple OBJECT.

Then do this

             GameObject bullit = Instantiate(ballPrefab, this.transform.position + point, Quaternion.identity) as GameObject;
             bullit.rigidbody.AddForce(Vector3.forward * 200.0f, ForceMode.Impulse);

Comment
Add comment · Show 14 · 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 Frogsama · Jan 30, 2015 at 10:31 PM 0
Share

Nope, is still not working :/

avatar image InvincibleCat · Jan 30, 2015 at 10:34 PM 0
Share

It is really hard to say without the project... Did you try with a more important value?

avatar image Frogsama · Jan 30, 2015 at 10:40 PM 0
Share

Well the project is pretty simple. You have a ball in the middle and if you click somewhere on the ground, the ball will spawn a smaller ball( aka bullet) the stuff above is just for finding the right spot with some vector math. So yeah the bulletPrefab is just a small sphere with no gravity but mass.

Yeah i tried even higher numbers but it is never moving on its own. If u bump in them they will move though.

avatar image InvincibleCat · Jan 30, 2015 at 10:43 PM 0
Share

What I meant by

It is really hard to say without the project...

Is more about your objects hierarchy, your components, your physics settings, etc...

avatar image Frogsama · Jan 30, 2015 at 10:44 PM 0
Share

I would love to send it to you somehow if you have time to look at it

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to use AddForce on FPC 0 Answers

Change player movement 0 Answers

Add force which throws back an object 3 Answers

Check if my AI is grounded 1 Answer

Can a object without a rigidbody be triggered by a trigger? 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