Making an Object Appear
Hi everyone, I am fairly new too unity so yeh, I was wondering how to get a object to be created once you left click, this object is a bullet which will fire towards the mouse click. SO when i left click how do i get this other object to show up?
Answer by toddisarockstar · Jan 21, 2016 at 03:07 PM
whenever possible its best not to create and destroy bullets during run time for proficiency purposes. i would make the bullet ahead of time and put it in the inspector.
lets assume you have an object named "player" in your game. and you have another object called "bullet"
here is a script you would attach to the "bullet" object:
var player:GameObject;
player=gameObject.Find("player");
var hit:RaycastHit;
var ray:Ray;
var target:Vector3;
var go:Vector3;
var speed:float;
speed=10;
function Update () {
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, target,speed*Time.deltaTime);
if (Input.GetMouseButtonDown(1)) {
transform.position=player.transform.position;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {target=hit.point;
transform.position=player.transform.position;
}
}
}
this would require a ground under your player with a box collider attached
Answer by inf3rno24 · Jan 21, 2016 at 03:39 PM
What do you mean here "this would require a ground under your player with a box collider attached "
Your answer
![](https://koobas.hobune.stream/wayback/20220612061054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving object between points 1 Answer
Why does my object's position change when I rotate it? 0 Answers
How to make two characters to go to the direction of an touched item on a single screen 0 Answers
Unexpected blur on fast moving object 1 Answer
Increase spawn object movement when player collects every 10 points 0 Answers