- Home /
问题已经得到解答,已经采纳了正确的解答
Why my object have velocity , but it can't move follow it's velocity?
if (Input.GetMouseButtonUp(0))
{
aimMode = false;
bulletRb.isKinematic = false;
bulletRb.velocity = -tempPos*vc;
print(bulletRb.velocity);
bull = null;
}
here ,I tried to follow this book to make a second game . I have a object named bullet , I get it's rigidbody : bulletRb, I set it's velocity when I press mouse button , I use a float value * a mouse position to point to the emitter position as the value of its speed ,but it doesn't work , it's motion like it's speed is 0,it just fall down because it's gravity ,but I print it's velocity , it have value . What's more strange is when I modify it's velocity from -tempPos*vc to tempPos*vc , It's back to normal.And it's the same result when I copy the source code from the book.
Answer by Icecoldless · Jul 29, 2018 at 01:58 AM
The reason of problem is I haven't open isTigger switch in my Launching object . So I only can let it go out at the edge of the emitter, and if the velocity was the opposite, it would hit the object's collider and cause the velocity to go to zero in an instant.
Answer by Bunny83 · Jul 28, 2018 at 05:51 PM
To me it looks like your "bullet" (which is public) actually holds a reference to a prefab. From the little code preview on the right of your image we can see you actually have another gameobject reference called "bull" which most likely is an instance of your prefab "bullet". However we don't see where you may instantiate that prefab. Are you sure you get the Rigidbody from your bull object and not from bullet? If you get the rigidbody from the prefab it won't affect the instantiated / cloned object.
We don't see enough of your code to give a more precise answer. Note you should rename your "tempPos" variable as it doesn't represent a "position" but a direction. Without seeing the preview code in the image i would have assumed it actually represents a position and that most likely would be wrong. It's extremely misleading calling a variable "banana" when you store the current time in it. Many people seem to be lazy when they name their variables. As long as you know what you mean by that it's fine. However as soon as others should work on your code it becomes a problem as the purpose of a code snippet is not clear to us if the variable names are misleading.
thank you answer my question,but I found the reason for the problem , I have instance of my prefab "bullet" in on$$anonymous$$ouseDown() method , I 'm sure I get the Rigidbody from my bull object and not form bullet , the reason is I haven't open isTigger switch in my Launching object . So I only can let it go out at the edge of the emitter, and if the velocity was the opposite, it would hit the object's collider and cause the velocity to go to zero in an instant.
if (Input.Get$$anonymous$$ouseButtonUp(0))
{
aim$$anonymous$$ode = false;
bulletRb.is$$anonymous$$inematic = false;
bulletRb.velocity = -tempPos * vc;
print(bulletRb.velocity);
}
this is how I test my velocity ,it only get velocity when I press mouse button , but in next frame , it hit emitter collider , so it's velocity will turn to zero in an instant.
Follow this Question
Related Questions
What is wrong with this rigidbody? 2 Answers
Not able to find the problem in my movement code using velocity 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
How to use AddForce and Velocity together 1 Answer
calculate velocity of animated parent rigidbody from script inside child 2 Answers