- Home /
Need some help with firing projectile at player using Rigidbody Velocity
Alright so i'm using the script below to stop the enemy from moving once the player enters a trigger attached to the enemy, look at the player and then fire projectiles at him. The problem here is that i can't make the enemy to fire the projectiles exactly at the position of the player. It's always near him but not exactly at him. Any advice? Maybe there's another way to do this without using Rigidbody and Velocity?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireAtPlayer : MonoBehaviour {
bool p_detected;
public Transform target;
public UnityEngine.AI.NavMeshAgent agent;
public Rigidbody projectile;
public Transform spawn;
public int speed;
// Use this for initialization
void Start () {
speed = 60;
}
// Update is called once per frame
void Update () {
p_detected = GetComponentInChildren<PlayerDetection>().player_detected;
if (p_detected)
{
target = GameObject.FindGameObjectWithTag("Player").transform;
GetComponent<walking>().enabled = false;
agent.isStopped = true;
transform.LookAt(target);
StartCoroutine(shooting());
}
if (!p_detected)
{
target = null;
GetComponent<walking>().enabled = true;
agent.isStopped = false;
}
}
IEnumerator shooting()
{
yield return new WaitForSeconds(3);
Rigidbody clone;
clone = (Rigidbody)Instantiate(projectile, spawn.position, transform.rotation);
clone.velocity = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
}
Ins$$anonymous$$d of having
Rigidbody projectile;
id have
GameObject projectile
but aside from that... does this happen if the player isn't moving? and have you tried stopping the editor and stepping through to see if the projectile is actually missing him or if it's going through him (because it's going too fast)?
The thing is that if i change Rigidbody to Gameobject i cannot use the Velocity function. No it's not going though him but near him.
Have you checked the player itself? Where is the center point? Is the player mesh offset and the bullet is going right where you tell it to? Replace the player object with a cube, do you still get the same result?
Answer by kalen_08 · Jul 19, 2018 at 02:37 PM
!! I think I got it.
It's because you're using different values for the initial position: You're using TRANSFORM.POSITION instead of CLONE.TRANSFORM.POSITION; meaning that the direction is wrong meaning it will always miss by a slight margin.
This is because your
public Transform spawn;
is obviously not the same as the transform or else you wouldn't have it.
The solution is:
IEnumerator shooting()
{
yield return new WaitForSeconds(3);
Rigidbody clone;
clone = (Rigidbody)Instantiate(projectile, spawn.position, transform.rotation);
clone.velocity = Vector3.MoveTowards(clone.transform.position, target.position, speed * Time.deltaTime);
}
I would also change it slightly:
IEnumerator shooting()
{
yield return new WaitForSeconds(3);
var clone = Instantiate(projectile, spawn.position, transform.rotation);
var dir = (player.transform.position - clone.transform.position).normalized;
clone.velocity = dir * speed;
}
hello? did this work? if so mark answer as correct and up vote it or if not then say something.
Hey. I haven't tested it yet since i'm away from my computer for the weekend.
I actually put a Input.Get$$anonymous$$eyDown function along the if (p_detected) line and it now doesn't fire 10000000 projectiles per second. That's because i use it in the Update method but i don't know to make it like this without using a key. About the direction of the projectile...well even with your solution it doesn't work as it should. If i use the $$anonymous$$eyCode and your solution it's actually less accurate than before!
I actually just used your second solution and for some reason it works perfectly! :O The accuracy is dead on! Just have to find a way to shoot one projectile without the use of a key.
Your answer
Follow this Question
Related Questions
Convert rigidbody velocity to home in on target when it gets close 1 Answer
Strange/Buggy code behavier 1 Answer
Velocity for movement 0 Answers
Pausing and Resuming a Rigidbody 1 Answer