- Home /
How to slow down a gameobject that has rigidbody
I have Player with a rigidbody player script that stops abruptly when i take my off the joystick. However i want it drift forward before stopping
[RequireComponent (typeof (Rigidbody))] public class PlayerController : MonoBehaviour {
public LayerMask groundLayers;
public CapsuleCollider CapCol;
private Vector3 JumpMovement;
private Rigidbody myRigidbody;
public Vector3 velocity;
[SerializeField] float jumpForce;
private AudioManager audioManager;
// variable to hold shaking acceleration value
Vector3 accelerationDir;
[SerializeField]private GameObject overheadtextSphere;
// Use this for initialization
void Start ()
{
myRigidbody = GetComponent<Rigidbody> ();
CapCol = GetComponent<CapsuleCollider> ();
audioManager = FindObjectOfType<AudioManager> ();
}
void Update()
{
// Read new acceleration Input from mobile device
accelerationDir = Input.acceleration;
/* If you shake the mobile device hard enough
(accelerations Square Magnitude greater then 5 for example)
if (accelerationDir.sqrMagnitude >= 4f)
{
Jump ();
} */
/*if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
}*/
}
void FixedUpdate()
{
myRigidbody.MovePosition (myRigidbody.position + velocity * Time.fixedDeltaTime);
/* if (Input.GetKeyDown (KeyCode.Space) && IsGrounded() )
{
}*/
}
public void Jump() { jumpForce = 10; if(IsGrounded()) { myRigidbody.AddForce (Vector3.up * jumpForce, ForceMode.Impulse); audioManager.Play ("Jump"); }
}
public bool IsGrounded()
{
//.9 is 90% the sphere, jump only if touch the ground
return Physics.CheckCapsule (CapCol.bounds.center, new Vector3 (CapCol.bounds.center.x, CapCol.bounds.min.y,
CapCol.bounds.center.z), CapCol.radius * .1f, groundLayers);
}
public Vector3 GetPosition()
{
return transform.position;
}
public void Move(Vector3 _velocity) {
velocity = _velocity;
}
public void LookAt(Vector3 lookPoint) {
Vector3 heightCorrectedPoint = new Vector3 (lookPoint.x, transform.position.y, lookPoint.z);
transform.LookAt (heightCorrectedPoint);
}
}
[RequireComponent (typeof (PlayerController))] public class Player : LivingEntity {
public float moveSpeed = 5f;
PlayerController controller;
Camera viewCamera;
[SerializeField] VirtualJoystick joystick;
[SerializeField] GameObject JoyStickbase;
// Use this for initialization
protected override void Start () { base.Start (); controller = GetComponent (); viewCamera = Camera.main;
if(Manager.Instance.sceneIndex == 5)
{
first_rank_label = KOmode.Instance.rank_text;
last_rank_label = KOmode.Instance.Lastrank_text;
playertag = this.gameObject.tag;
melee_bool = true;
}
}
void Update ()
{
Vector3 moveInput = new Vector3 (SimpleInput.GetAxis ("Horizontal"), 0, SimpleInput.GetAxis ("Vertical")); //controls looking with mouse
// Vector3 moveInput = new Vector3(joystick.Horizontal(),0,joystick.Vertical());
Vector3 moveVelocity = moveInput * moveSpeed;
controller.Move (moveVelocity);
/* if (Time.timeScale < 1) // To move fast during slowmotion
{
controller.Move (moveVelocity* 5); // time gets slowed by 20,so we multiply by 20
}*/
Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition);
Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(ray,out rayDistance)) {
Vector3 point = ray.GetPoint(rayDistance); // point of interception
controller.LookAt(point);
}
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch (0);
//Vector3 touchPosition = Camera.main.WorldToScreenPoint (touch.position);
JoyStickbase.transform.position = touch.position;
}
Vector3 namePos = Camera.main.WorldToScreenPoint (this.transform.position);
if(melee_bool)
{
if(playertag == KOmode.Instance._firstRankplayer)
{
//first_rank_label = KOmode.Instance.rank_text;
first_rank_label.transform.position = namePos;
}
if(playertag == KOmode.Instance._lastRankPlayer)
{
//last_rank_label = KOmode.Instance.Lastrank_text;
last_rank_label.transform.position = namePos;
}
}
}
}
Answer by xill · Aug 21, 2019 at 12:41 PM
If I understand right your moveVelocity gets its value from Input. So the value is 0 when there is no Input. Which makes your character
You should not call controller.Move (moveVelocity) all frames that's why your character suddenly stops as velocity becomes 0.
Call it when there is Input. So characters velocity won't be 0 if there is no Input and should decrase overtime with physics.
Your answer
Follow this Question
Related Questions
Rocket-styled angular drag 0 Answers
Making ball move on drag 0 Answers
Rigidbody movement 1 Answer