- Home /
How can I stop moving my player by AddExplosionForce after a while?
Hello, I'm using the function AddExplosionForce() and my player is been affecting quite a long time. Especially the position and rotation of my player are changing continuously.
I want to make my player is affected at the time of 'Explosion' like moving back and stopped after a while. In addition, the rotation should not be changed by the function.
What do you think about that?
Regarding rotation, you can freeze x, y, z rotations of your player rigid body. Regarding other issue, can you share some sample code and more elaboration of the scenario?
Thank you for your answer. Unfortunately, I have to use the rotation around the y-axis(so clockwise and counterclockwise). I found the rotation has occurred when the player collided with enemies or barrels.
player script:
[System.Serializable] public class Anim { public AnimationClip idle; public AnimationClip runForward; public AnimationClip runBackward; public AnimationClip runRight; public AnimationClip runLeft;
}
public class PlayerCtrl : $$anonymous$$onoBehaviour { private float h = 0.0f; private float v = 0.0f;
private Transform tr;
public float moveSpeed = 10.0f;
public float rotSpeed = 100.0f;
public Anim anim;
public Animation _animation;
public int hp = 100;
private int initHp;
public Image imgHpbar;
public delegate void PlayerDieHandler();
public static event PlayerDieHandler OnPlayerDie;
void Start()
{
tr = GetComponent<Transform>();
_animation = GetComponentInChildren<Animation>();
_animation.clip = anim.idle;
_animation.Play();
initHp = hp;
}
// Update is called once per frame
void Update()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
/*
Debug.Log("H=" + h.ToString());
Debug.Log("V=" + v.ToString());
*/
Vector3 moveDir = (Vector3.right * h) + (Vector3.forward * v);
tr.Translate(moveDir.normalized * moveSpeed * Time.deltaTime, Space.Self);
tr.Rotate(Vector3.up * rotSpeed * Time.deltaTime*Input.GetAxis("$$anonymous$$ouse X"));
You can also try modifying the drag on the player's rigidbody component.
Answer by Zoedingl · Jun 21, 2020 at 02:54 PM
You can increase the drag attribute in the player's Rigidbody.