Player teleports in a random direction
Made a script for interacting with an object called AmmoBox ... What the scripts SHOULD do : Spawn a bullet, test if the bullet hit the ammobox , if yes destroy the bullet and refill some ammo What the script isnt doing : Destroying the bullet What the scipt SHOULDNT do : Teleport the player -.- >.<
Script : Interaction Spawner :
using UnityEngine; using System.Collections;
public class InteractionSpawn : MonoBehaviour {
public GameObject InteractionSpawner;
public GameObject InteractionController;
public GameObject Player;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown("e"))
{
Instantiate(InteractionController, transform.position, transform.rotation);
}
}
} Script 2 : The Interactors "Physics"
using UnityEngine; using System.Collections;
public class InteractionScript : MonoBehaviour { public GameObject Interacter; public GameObject Player;
public float range = 0.7f;
public float speed = 0.5f;
public float AmmoSupply = 32;
RaycastHit hit;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
range -= 0.1f;
if (range <= 0)
{
Destroy(Interacter);
}
transform.Translate(0, 0, speed);
}
void FixedUpdate()
{
if (Input.GetKeyDown("e"))
{
Shooting AmmoReload = Player.GetComponent<Shooting>();
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out hit, range))
{
if (hit.transform.tag == "AmmoSupply")
{
AmmoReload.AmmoInInventory += AmmoSupply;
range = 0.1f;
}
}
}
}
}
Script 3 : The ammo box
using UnityEngine; using System.Collections;
public class AmmoPickup : MonoBehaviour { public GameObject Player; public GameObject AmmoBox; public GameObject destroyer;
public float AmmoRestore = 32;
void Start()
{
}
void Update()
{
}
void OnCollisionEnter(Collision other)
{
Shooting Ammo = Player.GetComponent<Shooting>();
if (other.gameObject.tag == "Interacter")
{
Ammo.AmmoInInventory += AmmoRestore;
destroyer.active = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612062550im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to teleport an object to 1 of 8 possible locations on collision? 1 Answer
How to make a raycast detect a trigger that its above of an object with mesh collider? 1 Answer
Field value changes after SceneManager.LoadScene been called 0 Answers
How to generate a raycast on keydown and store what it hits as a variable 0 Answers