I am trying to get a object to spawn at mouse position in unity 2d
whenever I click, but none of the objects are showing. It adds new clones in the hierarchy, but just not showing.
Here Is the script for the game controller object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gamecon : MonoBehaviour
{
public GameObject square;
public void Start()
{
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 spawnpos = Camera.main.WorldToScreenPoint(Input.mousePosition);
GameObject g =Instantiate(square, (Vector2)spawnpos, Quaternion.identity);
}
}
}
I cant find any answers on the web that apply to my situation.
Thanks for help.
The solution is to use the ScreenToWorldPoint method and not the WorldToScreenPoint because what you need is the world position to spawn your object.
Use the following code:
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 spawnpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject g =Instantiate(square, (Vector2)spawnpos, Quaternion.identity);
}
}
Related
I've tried to fix this problem for many days, looked for every result in a web and didn't get any suitable answers to me...
With this problem I can run my game, but it shows up every time I shoot the bullet prefab.
This is a player shooting script, it shows up a problem when I try to Instatiate(bulletRef);
public GameObject bulletRef;
// Start is called before the first frame update
void Start()
{
bulletRef = Resources.Load("Bullet") as GameObject;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
// By pressing the button we create an instance of a bullet
Instantiate(bulletRef);
bulletRef.transform.position = new Vector3(transform.position.x + .4f, transform.position.y + .2f, -1);
}
}
I think that those codes look suspicious to me, maybe the roots of this problem are coming out from them
here is a script for bullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
public float speed = 20f;
public int damage = 1;
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb.velocity = -transform.up * speed;
}
private void OnTriggerEnter2D(Collider2D collider)
{
Destruction enemy = collider.GetComponent<Destruction>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
Destroy(gameObject);
}
}
This is a script for destroying the bullet. I don't personally think that there is an issue with this code, but it is for you to understand how my bullet dissapears
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destruction : MonoBehaviour
{
public int health = 1;
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die()
{
Destroy(this.gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
ScoreManager.instance.AddPoint();
}
}
The problem lies in your Update() method of your first script
Instantiate(bulletRef);
bulletRef.transform.position = new Vector3(transform.position.x + .4f, transform.position.y + .2f, -1);
You are instantiating the gameobject bulletRef but not assigning it to any field. Also, you are changing the position of just a reference to the gameobject you just loaded from the resources. You didn't have a reference to the gameobject in the scene, you have a reference to the gameobject in your resources. To get a reference to the gameobject you just instantiated into your scene, assign the instantiated gameobject to a field. But the function Instantiate returns an Object hence we typecast it to GameObject as
GameObject bulletRefObject = Instantiate(bulletRef) as GameObject;
bulletRefObject.transform.position = new Vector3(transform.position.x + .4f, transform.position.y + .2f, -1);
I have a script that makes the camera do a shake by putting a button because
It is a public access function, if I do it that way when placing a button it works well but what I cannot achieve is to call the function so that every time my player collides with an enemy he makes the shake. I hope you can help me.
The shake code in camera is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenShaker : MonoBehaviour {
private float shakeAmount = 0.5f;
private float shakeTime = 0.0f;
private Vector3 initialPosition;
private bool isScreenShaking = false;
void Update () {
if(shakeTime > 0)
{
this.transform.position = Random.insideUnitSphere * shakeAmount + initialPosition;
shakeTime -= Time.deltaTime;
}
else if(isScreenShaking)
{
isScreenShaking = false;
shakeTime = 0.0f;
this.transform.position = initialPosition;
}
}
public void ScreenShakeForTime(float time)
{
initialPosition = this.transform.position;
shakeTime = time;
isScreenShaking = true;
}
}
The enemy code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControladorEnemigoCirculoMediano : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Here I don't know what to call the public void function ScreenShakeForTime (float time);
I already tried many things online but when my character comes into contact with the character, I don't do the shake in the camera.
}
}
}
You can create Unity-singleton in your ScreenShaker class like:
class ScreenShaker
{
public static ScreenShaker Instance {private set; get;};
void Awake() {
Instance = this;
}
}
And than from any place to call like:
ScreenShaker.Instance.ScreenShakeForTime(2f);
This is the easiest way, but maybe it's better to create standard singeleton(it's up to you).
And also don;t forget to destroy it on OnDestroy()
can you tell me in enemy game object collider isTrigger is enable or not
if it is not enable then use OnColliderEnter2D(Collision2D other){} for collision detection
I just want to move a bullet (but it doesn't work) and test on cube (but the code too did not move the cube).
The commented codes also did not move the game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movetest : MonoBehaviour
{
//public GameObject cube;
// Use this for initialization
public GameObject testmovecube;
public Rigidbody rb;
void Start () {
//testmovecube = this.GetComponent<GameObject>();
//rb = testmovecube.GetComponent<Rigidbody>();
move();
//
}
// Update is called once per frame
void Update () {
}
private void move()
{
testmovecube.transform.position =
Vector3.MoveTowards(transform.position, new Vector3(226, 1, 226) , 2);
//transform.Translate(Vector3.forward);
//rb.AddForce(Vector3.forward);
}
}
Please any help is appreciated.
I can't get my camera's position to move with the player.
This is the CameraController.cs
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject Player;
private Vector3 offset;
void Start()
{
transform.position = Player.transform.position;
}
void LateUpdate()
{
transform.position = Player.transform.position;
Debug.LogError(transform.position);
}
}
The script is a component of the main camera. The camera is not a child of the player object or vice versa.
The debugging says that the position is being updated to the player's position, but when the game is run the camera is static and doesn't move from its initial starting point.
Try this:
using UnityEngine;
using System.Collections;
public class CameraController: MonoBehaviour {
public GameObject Player;
private Vector3 offset;
void Start () {
offset = transform.position - Player.transform.position;
}
void LateUpdate () {
transform.position = Player.transform.position + offset;
}
}
The offset is distance between camera and player.
Another way is by making camera a child of player.
using UnityEngine;
using System.Collections;
public class CameraFollower : MonoBehaviour
{
public Transform thePlayer;
private Vector3 offset;
void Start()
{
offset = transform.position - thePlayer.position;
}
void Update()
{
Vector3 cameraNewPosition = new Vector3(thePlayer.position.x + offset.x, offset.y, thePlayer.position.z + offset.z);
transform.position = cameraNewPosition;
}
}
Thanks so much for posting and trying to help. The problem was that I was trying to get the script to move the camera when it was in a Virtual Reality supported environment. I have found that the way the camera behaves, and subsequently how the camera is moved, is different when it is in a VR environment and the camera needs to be a child of an object to be moved and cannot be moved via scripts.
I making simple runner game.
I want do have some block over which player which jump.
I make a prefab and quad.
Attached Spawn script:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 1f;
// Use this for initialization
void Start ()
{
Spawn();
}
void Spawn()
{
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
Also I attached Destroyer script:
using UnityEngine;
using System.Collections;
public class DestroyerScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
}
But when player enter this object nothing happens.
Screen of my Quad:
Where is my mistake?
You use OnTriggerEnter2D. If your collider not ticked isTrigger field, you can use OnCollisionEnter2D.
And also if your object has Normal (3D) collider you need to use 3D versions of them.
OnTriggerEnter or OnCollisionEnter.
And also you should read this.
UPDATE
After discussion and looking your project problem is your character doesnt hit the destroyer object's collider. It moves with your main camera. (Destroyer object is child object of camera). Because of that when you take your destroyer from camera's child object it works.
Any child object in the heirarchy inherits it's parents movement. So if the collider is on a child object of the camera, it will move when the camera moves.