- Home /
issue with the rigid body x axis ,addforce doesn't work as it should for x axis
This piece of code attached to a bullet game object that is supposed to get the distance between the other player and bullet and move the other player in the opposite direction only works properly along the Y axis. Basically when I shoot another player it just causes them to jump up. This is not to say that it doesn't move at all at the x axis, as the x axis of the player did show small change in the inspector, and when I lowered the mass to 0.001 the bullets made the object teleport a little bit on the x axis.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class bullet : MonoBehaviour
{
playercont victim;
private float speed = 5;
private Rigidbody2D rb;
float life = 4;
float dur = 5.5f;
float push = 1.5f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.up * speed;
}
private void FixedUpdate()
{
life -= Time.deltaTime;
if (life <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
Rigidbody2D enem = other.GetComponent<Rigidbody2D>();
if (enem!= null)
{
Vector2 dif = enem.transform.position - rb.transform.position;
dif = dif.normalized * push;
enem.AddForce(dif);
StartCoroutine(knocktime(enem));
Destroy(gameObject);
}
}
void Update()
{
}
private IEnumerator knocktime(Rigidbody2D enem)
{
if (enem != null)
{
yield return new WaitForSeconds(dur);
enem.velocity = Vector2.zero;
}
}
}
Keep in mind that while this is a platformer, I turned of gravity to make sure it's not friction, if it was working properly it would just float away, which it didn't.
,This piece of code attached to a bullet game object that is supposed to get the distance between the other player and bullet and move the other player in the opposite direction only works properly along the Y axis. Basically when I shoot another player it just causes them to jump up. This is not to say that it doesn't move at all at the x axis, as the x axis of the player did show small change in the inspector, and when I lowered the mass to 0.001 the bullets made the object teleport a little bit on the x axis.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class bullet : MonoBehaviour
{
playercont victim;
private float speed = 5;
private Rigidbody2D rb;
float life = 4;
float dur = 5.5f;
float push = 1.5f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.up * speed;
}
private void FixedUpdate()
{
life -= Time.deltaTime;
if (life <= 0)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
Rigidbody2D enem = other.GetComponent<Rigidbody2D>();
if (enem!= null)
{
Vector2 dif = enem.transform.position - rb.transform.position;
dif = dif.normalized * push;
enem.AddForce(dif);
StartCoroutine(knocktime(enem));
Destroy(gameObject);
}
}
void Update()
{
}
private IEnumerator knocktime(Rigidbody2D enem)
{
if (enem != null)
{
yield return new WaitForSeconds(dur);
enem.velocity = Vector2.zero;
}
}
}
Keep in mind that while this is a platformer, I turned of gravity to make sure it's not friction, if it was working properly it would just float away, which it didn't.
Answer by whamu · Apr 26 at 01:02 AM
yh turns out it was because I was setting the rigidbody velocity on the player script.
Your answer
Follow this Question
Related Questions
knockback freezing game 0 Answers
How do I get ForceMode2D.Impulse to work? 2 Answers
AddForce knockback x axis according to mouse.position more then y axis (2D game) 0 Answers
knockback 3d c# 1 Answer