I want to make it so that the blood effect in my game shoots out the back of the player relative to what direction the bullet was traveling. I want it to rotate only on one axis because my game is a top-down shooter, but everything I’ve tried so far has been glitchy and unreliable. What can I do to fix this?
What have you tried?
Did you try rotating the game object the particle emitter is attached to?
Depending on how you detect the bullet hit f.E. with a Raycast you could use the RaycastHit.normal.
Using OnCollision/Trigger … works the same just with contactPoint.normal
I have tried rotating the game object, but what I’m not sure about is which rotation function to use or how to use it, every function and parameters I’ve used have ended up rotating it in unintended ways
OK, but we’ll need more specific info on how you’re trying to solve this (even if done so incorrectly), then that’d help us help you. I’m not even sure how you currently have your scene structured.
How are you detecting the bullet? Do you have a reference to the bullet? If so, you can look back along the bullet’s direction. Are you using raycasting? If so, you can use the raycast hit result to figure out the direction. Collision? How are you getting the bullet info?
Then you can use transform.LookAt(direction) on the particle emitter’s game object.
(sorry for the late response) This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBulletEffects : MonoBehaviour
{
public Transform Player;
public Transform Enemy;
public Rigidbody rigid;
public ParticleSystem Blood;
public float bulletSpeed;
public float DamageT;
static public float damage;
public int bloodCount;
// Use this for initialization
void Start()
{
Enemy = this.transform.parent;
this.transform.parent = null;
damage = DamageT;
rigid = GetComponent<Rigidbody>();
rigid.AddRelativeForce(Vector3.forward * bulletSpeed, ForceMode.VelocityChange);
}
private void OnTriggerEnter(Collider other)
{
if (other.tag != "Enemy" && other.tag != "Player" && other.tag != "Bullet")
{
Destroy(this.gameObject);
}
if (other.tag == "Player")
{
Blood.transform.position = other.transform.position;
Blood.transform.rotation = Quaternion.LookRotation(Enemy.position - Player.position, Vector3.down);
Blood.Emit(bloodCount);
Destroy(this.gameObject);
}
}
}
OK, I’m assuming this script is attached to the bullet.
Blood.transform.rotation = Quaternion.LookRotation(Enemy.position - Player.position, Vector3.down);
I suspect what’s happening here (and possibly the cause of your problems) is that the enemy may always be moving around while the bullet is in flight. By the time the bullet hits the player, who knows where the enemy is?
So the solution is to simply remove the enemy from the equation. Once fired, the bullet no longer has a relationship to the enemy. It’s its own object now.
With that said…
In this script, “transform” in this script refers to the bullet’s transform. This also means “transform.forward” gives you the bullet’s facing direction, which is probably what you want to use to set the particle’s direction (i.e. use the negative “-transform.forward” to have the blood spurt backwards behind the bullet.