3
\$\begingroup\$

I am at 1.2 stage (moving the player) at Unity course "Roll-a-ball" and have question:

I want to add feature that stops a player and sets his new position on 0, 0, 0.

I can reset the position, but I have really problem with stopping a player. The ball won't stop rolling.

My code is:

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

        private Rigidbody rb;
        public float speed = 1.0f;

        void Start()
        {
            rb = GetComponent<Rigidbody>();
        }

        void FixedUpdate()
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");

            Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

            rb.AddForce(movement*speed);
            if (Input.GetKeyDown(KeyCode.Space))
            {
                rb.position = new Vector3(0,0.5f,0);
            }
            print(movement);
        }
    }

What I tried?

  • Adding new vector3 (0,0,0) in if section,
  • New vector3 that is opposite to movement,
  • .Stop after ascribing new position to (0,0,0), but it 'freezes' before (example: I click space when sphere is at board's edge, then it freezes there, when I click arrow it go to new position and start going).

So, how to do that? I have no clue right now, although problem seems be easy.

Here a image with code and Unity with opened Inspector on object. enter image description here

EDIT

I've a another problem.

Changed code as below (as Byte56 suggested):

if (Input.GetKeyDown(KeyCode.Space))
{
    speed = 0;
    rb.velocity = Vector3.zero;
    rb.transform.position = new Vector3(0, 0.5f, 0);
    rb.constraints = RigidbodyConstraints.FreezeAll;
    speed = 1.0f;
}
if (moveHorizontal != 0 || moveVertical != 0)
{
    rb.constraints = RigidbodyConstraints.None;
}

It seems like clicing space not always freezes object. It always put it on right place, but sometimes doesn't freeze. No matter if I use code rb.constraints = RigidbodyConstraints.FreezeAll; or rb.Sleep(); - sometimes it works, sometimes not. I can't prove it, but it seems the smaller is delta of time between clicking space (smaller than 1 second) the chance that it won't freeze increases. It's not fault of space, I tried it with NumEnter. Any idea? It seems really buggy.

\$\endgroup\$
  • \$\begingroup\$ I recreated this scene on my own. In about 5 minutes of testing I never encountered a time when pressing space didn't result in the expected result. If you're still having trouble, you should post as a new question, not continuing to edit this one. \$\endgroup\$ – MichaelHouse Apr 9 '16 at 20:48
2
\$\begingroup\$

If you want to stop a physics object from moving, you need to zero the forces and the velocity of the RigidBody.

rb.velocity = new Vector3(0,0,0);

For zeroing the force, you can apply a force opposite to the forces you've added that frame already. However, I'd suggest simply not adding those forces if you're stopping. It means a slight rearrangement of your code.

if (Input.GetKeyDown(KeyCode.Space))
{
    rb.position = new Vector3(0,0.5f,0);
    rb.velocity = new Vector3(0,0,0);
} else {
    rb.AddForce(movement*speed);
}

The ultimate way to stop the ball, activating all the constraints on the rigidbody.

if(moveHorizontal != 0 || moveVertical != 0) //for starting the movement again
    rb.constraints = RigidbodyConstraints.None;

if (Input.GetKeyDown(KeyCode.Space))
{
    rb.gameObject.transform.position = new Vector3(0,0.5f,0);
    rb.constraints = RigidbodyConstraints.FreezeAll;
} else {
    rb.AddForce(movement*speed);
}
\$\endgroup\$
0
\$\begingroup\$

It is due to constantly applying AddForce in FixedUpdate. You can easily do it by assigning zero to speed and set velocity to zero.

rb.AddForce(movement*speed);
        if (Input.GetKeyDown(KeyCode.Space))
        {
            speed = 0;
            rb.AddForce(-rb.velocity);
            rb.velocity = Vector3.zero;
            rb.position = new Vector3(0,0.5f,0);
        }

// Change speed from zero to any other value when you want to move it again.
\$\endgroup\$
  • \$\begingroup\$ As below - it doesn't stop immediately, but slowing and slowing and slowing sphere. The second thing is that setting speed to zero means I can't move this sphere again after clicking 'space'. \$\endgroup\$ – Szkaplerny Apr 8 '16 at 17:06
  • \$\begingroup\$ on clicking space you can set speed to 1.0f again \$\endgroup\$ – Hamza Hasan Apr 8 '16 at 17:07
  • \$\begingroup\$ nah, I didn't think about it, of course you are right. Any idea how to deal with my problem another than .velocity? \$\endgroup\$ – Szkaplerny Apr 8 '16 at 17:11
  • \$\begingroup\$ I think you are setting velocity or adding force anywhere else in your code \$\endgroup\$ – Hamza Hasan Apr 8 '16 at 17:13
  • \$\begingroup\$ That's my whole code, there is link in first line of my post to this course, I'm doing everything as in video. Now I look at my Inspector and there aren't any values matching this problem. If you want I can put here photo of Unity panels. \$\endgroup\$ – Szkaplerny Apr 8 '16 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.