RigidBody_problem with gameobject
Hello, I have a little problem. I want to instanciate an GameObject (Sphere) at runtime to be on my terrain. I used the Instanciate method but when i want to create that sphere at runtime, a sphere appears twice or i want a sphere to appear once. Do you have any ideas ? here is my code
using UnityEngine;
using System.Collections;
public class create_marble : MonoBehaviour {
public GameObject sphere;
public Camera camera;
void OnGUI()
{
if (Input.GetKeyDown(KeyCode.E))
{
Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
//sphere.transform.localScale += new Vector3(2, 2, 2);
Instantiate(sphere, pos, Quaternion.identity);
}
}
}
Answer by Veerababu.g · May 31, 2016 at 04:41 AM
use getkeydown instead of get key.get key down works only one time but get key works upto you release the key. it's like step. and one more thing use different different position to instantiate. if you are new to unity then take look at unity tutorials
thanks it's working with getkeydown .well i want to instantiate the object in front of the camera. and i guessed this is the best way to do what i want by using Vector3 pos = camera.transform.position + camera.transform.forward * 5f; isnt it ?
Answer by root_2 · May 30, 2016 at 09:36 AM
here is a screen capture of my problem
Oh right ..... i changed the void onGUI() and put void Update and it works fine for me
I spoke too fast it works with one object but if i want to add more object like (cube,capsule,cylinder).Like when i press the button for the cube the local_size is 10 times bigger than what i expected and the rigid_body make the object appears a lot. I modify the code with the Update method ins$$anonymous$$d of the OnGUI(); here is the modify version of the code
using UnityEngine;
using System.Collections;
public class create_marble : $$anonymous$$onoBehaviour {
public GameObject sphere;
public GameObject cube;
public GameObject Capsule;
public GameObject cylinder;
public Camera camera;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
Instantiate(sphere, pos, Quaternion.identity);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.H))
{
Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
Instantiate(cube, pos, Quaternion.identity);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.L))
{
Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
Capsule.transform.localScale += new Vector3(2, 2, 2);
Instantiate(Capsule, pos, Quaternion.identity);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.P))
{
Vector3 pos = camera.transform.position + camera.transform.forward * 5f;
cylinder.transform.localScale += new Vector3(2, 5, 2);
Instantiate(cylinder, pos, Quaternion.identity);
}
}
}
Your answer
Follow this Question
Related Questions
[Solved] How to Script a Camera That Follows the Player but Collides with the Edge of the Level 2 Answers
localscale not working 1 Answer
Camera Jitters When Displacing and Rotating Smoothly 0 Answers
My sprite disappears from the camera view after some time moving 5 Answers
NullReferenceException: Object reference not set to an instance of an objectInt32) 1 Answer