- Home /
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);
}
}
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.
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 :(
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);
It is really hard to say without the project... Did you try with a more important value?
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.
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...
I would love to send it to you somehow if you have time to look at it
Your answer
Follow this Question
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