Hi I want my prefab projectile to spawn at player2 position but it spawns a little ways away to the right. if facing my objects. I tried
projectile.transform.position = new Vector3(spawnX, spawnY, spawnz)
and
projectile.transform.position = Player2.transform.position
and
projectile.tansform.position = this.transform.position.rotation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class APIJumpWirhGrav : MonoBehaviour
{
public bool attacking = false;
public int playerHeight = 21;
public bool onTopOfPlayer = false;
private GameObject Player;
public Animator anim;
public Transform target;
public UnityEngine.AI.NavMeshAgent agent;
public GameObject playerRefrefrince;
private bool inJumpZone = false;
private Vector3 jumpHeight = Vector3.up * 4;
private GameObject Player2;
private CharacterController control;
private SphereCollider InJumpZone;
public GameObject tarrget;
private CharacterController controller;
private Rigidbody rb;
public Transform objectToFollow;
private float speed = 15f;
public GameObject TarrgetPos;
public float gravity = 20.0f;
public float targetTime = 100f;
BoxCollider collider2;
GameObject prefab;
public Vector3 forceMag = new Vector3(0, -900f, 50f);
void Start()
{
rb = GetComponent<Rigidbody>();
Player = GameObject.FindWithTag("Player");
Player2 = GameObject.FindWithTag("Player2");
prefab = Resources.Load("projectile") as GameObject;
collider2 = GetComponent<BoxCollider>();
}
void Update()
{
if (attacking == true)
{
targetTime -= 10 * Time.deltaTime;
if (targetTime <= 0.0f)
{
timerEnded();
agent.isStopped = false;
}
}
if (Input.GetButtonDown("Fire1") && onTopOfPlayer == true)
{
float spawnX = transform.position.x;
float spawnY = transform.position.y;
float spawnz = transform.position.z;
GameObject projectile = Instantiate(prefab) as GameObject;
projectile.transform.position = new Vector3(spawnX, spawnY, spawnz);
agent.isStopped = true;///////////////
// gameObject.transform.parent = gameObject.transform.parent = null;
inJumpZone = false;
attacking = true;
// GetComponent<BoxCollider>().enabled = true;
anim.SetBool("Jumping", true);
anim.SetBool("IsMoving", false);
Destroy(Player2);
}
if (onTopOfPlayer == true && attacking == false)
{
agent.isStopped = true;///////////
// GetComponent<BoxCollider>().enabled = true;
transform.position = Vector3.Lerp(transform.position, target.transform.position, 1f);
transform.rotation = Player.transform.rotation;
anim.SetBool("Jumping", false);
anim.SetBool("IsMoving", false);
}
if (inJumpZone && attacking == false)
{
// GetComponent<BoxCollider>().enabled = true;
transform.position = Vector3.Lerp(transform.position, target.transform.position + jumpHeight, .50f);
transform.rotation = Player.transform.rotation;
anim.SetBool("Jumping", true);
anim.SetBool("IsMoving", false);
onTopOfPlayer = true;
}
else
if (inJumpZone == false && attacking == false)
{
{
agent.SetDestination(target.position);
anim.SetBool("IsMoving", true);
anim.SetBool("Jumping", false);
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && attacking == false)
{
inJumpZone = true;
anim.SetBool("IsMoving", false);
anim.SetBool("Jumping", true);
}
}
void timerEnded()
{
attacking = false;
onTopOfPlayer = false;
targetTime = 100f;
}
void DestroyGameObject()
{
Destroy(gameObject);
}
}