- Home /
Stop momentum of a ball
Hello, well like the title says, I'm making a simple game, and I want to stop the momentum of a ball.
When I make the input to move the ball simply goes forward, and when it respawns the momentum stays in a loop, like "Portal" game, I did a research of how to do it and didn't find anything useful
Here's my code (C#):
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Vector3 spawn;
void Start()
{
spawn = transform.position;
}
void FixedUpdate()
{
float moveHor = Input.GetAxis ("Horizontal");
float moveVert = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (moveHor,0.0f, moveVert);
rigidbody.AddForce (move * speed * Time.deltaTime);
if(transform.position.y <= -4)
{
transform.position = spawn;
}
}
}
I did try the code
rigidbody.velocity = Vector3.zero; rigidbody.AddForce = Vector3.zero; rigidbody.angularDrag = Vector3.zero;
I did try making more variables, and same results
It didn't work
Thanks for reading
Without seeing your code, it is difficult to see where you went wrong. rigidbody.velocity = Vector3.zero applied in the right place(s), should do the job. You can also set is$$anonymous$$inematic to true to stop something.
The code is not showing??
using UnityEngine; using System.Collections; public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { public float speed; private Vector3 spawn; void Start() { spawn = transform.position; } void FixedUpdate() { float moveHor = Input.GetAxis ("Horizontal"); float moveVert = Input.GetAxis ("Vertical"); Vector3 move = new Vector3 (moveHor,0.0f, moveVert); rigidbody.AddForce (move speed Time.deltaTime); if(transform.position.y <= -4) { transform.position = spawn; } } }
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed;
private Vector3 spawn;
void Start()
{
spawn = transform.position;
}
void FixedUpdate()
{
float moveHor = Input.GetAxis ("Horizontal");
float moveVert = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (moveHor,0.0f, moveVert);
rigidbody.AddForce (move * speed * Time.deltaTime);
if(transform.position.y <= -4)
{
transform.position = spawn;
}
}
}
I wasn't clear. The script you posted is fine, but no where in the script do you use rigidbody.velocity = Vector3.zero. So without seeing the context where you used this line, I cannot say why it did not work for you. And even seeing the code in context may not be enough information for an answer.
Well I don't know exactly where to use the line of code, I used it in here:
if(transform.position.y <= -4)
{
transform.position = spawn;
rigidbody.velocity = Vector3.zero;
}
Is it ok? where can I use that?
Answer by _Yash_ · Oct 22, 2014 at 07:32 AM
yes doing
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
can fix it BUT here your input is adding force so make sure there is no input when it resets. You can also try disabling userInput for a couple of seconds after reset to see if it actually resets.
I tested your code in Unity right now, and for me it works with this FixedUpdate() $$anonymous$$ethod that other people (for instance Yash) has suggested.
void FixedUpdate()
{
float moveHor = Input.GetAxis ("Horizontal");
float moveVert = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (moveHor,0.0f, moveVert);
rigidbody.AddForce (move * speed * Time.deltaTime);
if(transform.position.y <= -4)
{
transform.position = spawn;
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}
}
If this is not working for you, do you have any other code in your project that might affect the current code?
Btw: you have to let go of the key when the ball is falling, if you keep pressing the key the ball, with this code, will of course be given force all the time and keep rolling.
After some time I could write those lines, and yes, I added some code after I posted this thread, thanks for the help
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Scripts stop working in Maximize On View and testing on Android 0 Answers
What Am I Doing Wrong? 1 Answer
Missile Firing Script Help 1 Answer
Vehicle Script Modifications 0 Answers