I can't manage to instantiate a prefab at my mouse position.
I've tried to instantiate the prefab at the current mouse position, but on click, the block shows in the hierarchy and not the scene. It also creates 4-5 prefabs.
using UnityEngine;
public class Building : MonoBehaviour
{
public GameObject block;
void Update()
{
if (Input.GetMouseButton(0))
{
Instantiate(block, new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f), Quaternion.identity);
}
}
}
I want to create 1 prefab of the block, and I want it to show up in the scene view.
Input.mousePosition is the coordinates of the mouse on the screen. Use Camera.ScreenToViewportPoint to get the world position.
The block will not show in the scene, because its position is likely something like (500, 300, 0), which is very far. Select the block in the Hierarchy and press "F" to see it.
Input.GetMouseButton() keeps firing as long as the mouse is held. Change this to Imput.GetMouseButtonDown()
using UnityEngine;
public class Building : MonoBehaviour {
public GameObject block;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
Instantiate(block, pos, Quaternion.identity);
}
}
}
You need to convert from screen space to world space.
One way to do this is to use Camera.ScreenToWorldPoint:
private Camera mainCam;
void Start()
{
mainCam = Camera.main;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 blockPos = mainCam.ScreenToWorldPoint(Input.mousePosition);
Instantiate(block, blockPos, 0f), Quaternion.identity);
}
}
If you want to spawn further away from the camera, look into Camera.ScreenPointToRay.
Related
I'm making a top-down game where you drive a car and shoot targets at the same time. I have a script that makes a sprite of a crosshair follows the mouse cursor and I want to have it set up so that when the player presses the mouse button (the mouse button isn't in the code now) and the crosshair sprite is overlapping an enemy sprite, the enemy dies. I was following this documentation on Bounds.Intersects. Here's my code:
public class shootingScript : MonoBehaviour
{
public GameObject target, enemy;
CircleCollider2D targetCollider;
CapsuleCollider2D enemyCollider;
// Start is called before the first frame update
void Start()
{
//Check that the first GameObject exists in the Inspector and fetch the Collider
if (target != null)
{
print("targ not null");
targetCollider = target.GetComponent<CircleCollider2D>();
}
//Check that the second GameObject exists in the Inspector and fetch the Collider
if (enemy != null)
{
print("enemy not null");
enemyCollider = enemy.GetComponent<CapsuleCollider2D>();
}
}
// Update is called once per frame
void Update()
{
if (targetCollider.bounds.Intersects(enemyCollider.bounds))
{
print("hit");
Destroy(enemy);
}
}
}
In-game "targ not null" and "enemy not null" prints but when I move my cursor and the crosshair goes over the enemy "hit" is not printed and the enemy is not destroyed. I have a CircleCollider2D on the crosshair and a CapsuleCollider2D on the enemy. The script is on an empty game object. I also tried sprite.bounds but that resulted in the enemy getting killed as soon as I run the game.
EDIT:
Here's the code that keeps the crosshair sprite on the cursor. I copied it from somewhere. I set moveSpeed to 99999 since I want the crosshair sprite to be exactly where the mouse is.
public class mouseReticle : MonoBehaviour
{
private Vector3 mousePosition;
public float moveSpeed = 0.1f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
}
}
The target sprite and enemy sprite were at different depths (z).
I've added a cube object with position (0, 0, 0) and sphere object with position (0.5, 0, -3). I've added textures and materials.
I want to move the sphere behind of all other objects with render queue or different solution.
I've added this script on the sphere. It's not working:
using UnityEngine;
public class RenderQueueTest : MonoBehaviour
{
public int renderQueuePosition = -1;
void Start()
{
GetComponent<Renderer>().material.renderQueue = renderQueuePosition;
}
}
I've also tried to change the sphere material render queue in the inspector.
Default render queue of the sphere material is: 2000
I changed it to: 1999
Doesn't work.
Here are the textures:
What you could do is try to move the sprite itself back, set the Transform position.y to -0.1 in the inspector or do it with a script like this:
using UnityEngine;
public class SendBackScrpt: MonoBehaviour
{
private Vector3 sendBack = new Vector3(0, -0.1f, 0);
private void Start()
{
Vector3 prevPos = transform.position;
transform.position = prevPos + sendBack;
}
}
what you could also try is putting the sprite above the other one in the hierarchy.
I am trying to set up control of Player game object by UI buttons.
2d top down view scene.
I want to transfer Player object smoothly on fixed distance (0.8 for Axix X for left-right direction and 2.4 for up-down)when I release left/right/up/down ui buttons.
Here i found code for smooth movement but there Player is moving all the time while ui button is pressed.
Can you help me to make it move on mouse up (pointer up) and to move for public x= 0.8f for left/right, and public y = 2.4f for up/down
And at the same time i want to use different speed (peblic) for moves on x and y Axis
Its ok if it should be totally other script using smth like transform.translate
Kindly guide to for any possible solution for this. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerControl : MonoBehaviour
{
float movX;
float movY;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movX = CrossPlatformInputManager.GetAxisRaw("Horizontal");
movY = CrossPlatformInputManager.GetAxisRaw("Vertical");
rb.velocity = new Vector2(movX * 1, movY * 1);
}
}
This script can be moved by the WASD keys.
This should move your gameObject by the requested amount over x amount of time(speed).
Currently it can be only moved when it'S reached its destionation but you can easily modify this :), by stopping the coroutine
using System.Collections;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
// We gonna move by WASD keys
[Header("Speed & Movement settings")]
[SerializeField]
float Speed = 2.0f;
[SerializeField]
float movSpeedX = 0.8f;
[SerializeField]
float movSpeedY = 2.4f;
bool ReachedTarget = true;
void Update()
{
if (ReachedTarget)
{
Vector2 dest = Vector2.zero;
Vector2 currentPos = transform.position;
if (Input.GetKeyUp(KeyCode.W))
{
dest = currentPos + (Vector2.up * movSpeedY);
StartCoroutine(moveTo(dest, Speed));
}
else if (Input.GetKeyUp(KeyCode.S))
{
dest = currentPos + (Vector2.down * movSpeedY);
StartCoroutine(moveTo(dest, Speed));
}
else if (Input.GetKeyUp(KeyCode.D))
{
dest = currentPos + (Vector2.right * movSpeedX);
StartCoroutine(moveTo(dest, Speed));
}
else if (Input.GetKeyUp(KeyCode.A))
{
dest = currentPos + (Vector2.left * movSpeedX);
StartCoroutine(moveTo(dest, Speed));
}
}
}
// Time to take is in seconds
IEnumerator moveTo(Vector2 TargetPosition, float TimetoTake)
{
Vector2 originalPosition = transform.position;
float Time_taken = 0f;
ReachedTarget = false;
while (Time_taken < 1)
{
Time_taken += Time.deltaTime / TimetoTake;
// Interpolating between the original and target position this basically provides your "speed"
transform.position = Vector2.Lerp(originalPosition, TargetPosition, Time_taken);
yield return null;
}
Time_taken = 0;
transform.position = TargetPosition;
ReachedTarget = true;
}
}
I can't find any documentation of CrossPlatformInputManager, and I know nothing about it. But if you need to get the event of "release a key" instead of "press the key", try this: Input.GetKeyUp.
Description
Returns true during the frame the user releases the key identified by
name.
You need to call this function from the Update function, since the
state gets reset each frame. It will not return true until the user
has pressed the key and released it again.
For the list of key identifiers see Conventional Game Input. When
dealing with input it is recommended to use Input.GetAxis and
Input.GetButton instead since it allows end-users to configure the
keys.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void Update()
{
if (Input.GetKeyUp("space"))
{
print("Space key was released");
}
}
}
If you want to stop the rigid body, you need to reset its velocity to
zero. Or you can use
Rigidbody2D.MovePosition
to move it a certain distance.
Parameters
position The new position for the Rigidbody object.
Description
Moves the rigidbody to position.
Moves the rigidbody to the specified position by calculating the
appropriate linear velocity required to move the rigidbody to that
position during the next physics update. During the move, neither
gravity or linear drag will affect the body. This causes the object to
rapidly move from the existing position, through the world, to the
specified position.
Because this feature allows a rigidbody to be moved rapidly to the
specified position through the world, any colliders attached to the
rigidbody will react as expected i.e. they will produce collisions
and/or triggers. This also means that if the colliders produce a
collision then it will affect the rigidbody movement and potentially
stop it from reaching the specified position during the next physics
update. If the rigidbody is kinematic then any collisions won't affect
the rigidbody itself and will only affect any other dynamic colliders.
2D rigidbodies have a fixed limit on how fast they can move therefore
attempting to move large distances over short time-scales can result
in the rigidbody not reaching the specified position during the next
physics update. It is recommended that you use this for relatively
small distance movements only.
It is important to understand that the actual position change will
only occur during the next physics update therefore calling this
method repeatedly without waiting for the next physics update will
result in the last call being used. For this reason, it is recommended
that it is called during the FixedUpdate callback.
Note: MovePosition is intended for use with kinematic rigidbodies.
// Move sprite bottom left to upper right. It does not stop moving.
// The Rigidbody2D gives the position for the cube.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public Texture2D tex;
private Vector2 velocity;
private Rigidbody2D rb2D;
private Sprite mySprite;
private SpriteRenderer sr;
void Awake()
{
sr = gameObject.AddComponent<SpriteRenderer>();
rb2D = gameObject.AddComponent<Rigidbody2D>();
}
void Start()
{
mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
velocity = new Vector2(1.75f, 1.1f);
sr.color = new Color(0.9f, 0.9f, 0.0f, 1.0f);
transform.position = new Vector3(-2.0f, -2.0f, 0.0f);
sr.sprite = mySprite;
}
void FixedUpdate()
{
rb2D.MovePosition(rb2D.position + velocity * Time.fixedDeltaTime);
}
}
Both documents have an example.
Or you don't want to user keyboard but UI buttons, try this: IPointerDownHandler and IPointerUpHandler
Description
Interface to implement if you wish to receive OnPointerDown callbacks.
Detects ongoing mouse clicks until release of the mouse button. Use
IPointerUpHandler to handle the release of the mouse button.
//Attach this script to the GameObject you would like to have mouse clicks detected on
//This script outputs a message to the Console when a click is currently detected or when it is released on the GameObject with this script attached
using UnityEngine;
using UnityEngine.EventSystems;
public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
//Detect current clicks on the GameObject (the one with the script attached)
public void OnPointerDown(PointerEventData pointerEventData)
{
//Output the name of the GameObject that is being clicked
Debug.Log(name + "Game Object Click in Progress");
}
//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
Debug.Log(name + "No longer being clicked");
}
}
I am creating a game that shifts the player between an isometric overhead view of the map, into a perspective view. The player starts by looking at the map as such,
Isometric view of map | Scene A
Upon clicking either of the red orbs floating, the player shifts into a separate scene in the perspective view at the orb location. Shown here,
Perspective 1 | Scene B
Perspective 2 | Scene B
The isometric map and the perspective maps are different scenes and it is important that they remain as such. The player needs to be able to click on an orb in Scene A and then have the camera move to the set position of the orb in Scene B.
My current thought is that each orb could have a tag (1,2,...) and once clicked, this tag would be referenced with an if statement to position the camera in the other scene. My problem is that I don't know how to reference the tag of the clicked orb once I am in the next scene. Or how I would reset this back to normal if I return to Scene A.
Here is some code that I have started, but I am not sure if I am on the right path.
using UnityEngine;
using UnityEngine.SceneManagement;
public class TagCheck : MonoBehaviour
{
Camera m_MainCamera;
public string sceneName;
int count;
private void Start()
{
m_MainCamera = Camera.main;
}
void LoadScene()
{
SceneManager.LoadScene(sceneName);
DontDestroyOnLoad(gameObject);
}
void OnMouseDown()
{
LoadScene();
Debug.Log(count);
}
void OnEnable()
{
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
void OnDisable()
{
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
if (count == 1)
{
if (GameObject.FindWithTag("1"))
{
m_MainCamera.transform.position = new Vector3(5f, 1.5f, 5f);
}
else if (GameObject.FindWithTag("2"))
{
m_MainCamera.transform.position = new Vector3(-5f, 1.5f, -5f);
}
}
else
{
Destroy(gameObject);
}
}
Thank you all for the help! Let me know if I need to clarify anything.
I was able to come up with a solution to this problem. I have my scene change orbs follow me into the next scene by attaching a DontDestroyOnLoad script to them. In the next scene, there is an empty game object that continuously checks for objects with specific tags. Once found, the object tells the checker to move the camera to my desired empty parent and then reset the coordinates back to 0. Here is the checker script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tagChecker : MonoBehaviour {
Camera m_MainCamera;
void Start()
{
m_MainCamera = Camera.main;
}
void TagCheck()
{
if (GameObject.FindWithTag("1"))
{
m_MainCamera.transform.SetParent(GameObject.Find("Perspective1").transform); //GameObject.Find("A").transform.parent;
m_MainCamera.transform.localPosition = new Vector3(0f, 0f, 0f);
m_MainCamera.transform.localRotation = Quaternion.identity;
Destroy(GameObject.FindWithTag("1"));
}
else if (GameObject.FindWithTag("2"))
{
m_MainCamera.transform.SetParent(GameObject.Find("Perspective2").transform); //GameObject.Find("A").transform.parent;
m_MainCamera.transform.localPosition = new Vector3(0f, 0f, 0f);
m_MainCamera.transform.localRotation = Quaternion.identity;
Destroy(GameObject.FindWithTag("2"));
}
else if (GameObject.FindWithTag("3"))
{
m_MainCamera.transform.SetParent(GameObject.Find("Perspective3").transform); //GameObject.Find("A").transform.parent;
m_MainCamera.transform.localPosition = new Vector3(0f, 0f, 0f);
m_MainCamera.transform.localRotation = Quaternion.identity;
Destroy(GameObject.FindWithTag("3"));
}
else if (GameObject.FindWithTag("4"))
{
m_MainCamera.transform.SetParent(GameObject.Find("Perspective4").transform); //GameObject.Find("A").transform.parent;
m_MainCamera.transform.localPosition = new Vector3(0f, 0f, 0f);
m_MainCamera.transform.localRotation = Quaternion.identity;
Destroy(GameObject.FindWithTag("4"));
}
}
void Update()
{
TagCheck();
}
}
Thanks,
I would abstract your level data information into a JSON data file or ScriptableObject instead of placing it directly into your code.
You could also have a "TeleportTo" Vector3 field and "SceneTo" Scene field on your orb object that the code can reference.
I'm trying to develop a simple shooting mechanism which instantiates a 'shot' based on the angle of rotation of the shooter. My problem here is even though the shooter is oriented at 0*, the shot fires at an angle of 45*(this is what I'm guessing the problem is because when the shooter is oriented at 45*, the shot is fired at exact 90*).
Shooter angle(0,0,0)
Shooter angle(0,0,45)
Note- The ball always launches from the center of the black flattened cylinder object.
Required Code:
public class ShotMoveScript : MonoBehaviour {
public static float xForce;
public Transform shott;
void Update () {
if(Input.GetKey(KeyCode.Q))
{
transform.Rotate(Vector3.forward, 5f);
}
if(Input.GetKey(KeyCode.E))
{
transform.Rotate(Vector3.forward, -5f);
}
if(Input.GetKey(KeyCode.Space))
{
xForce += 0.2f;
}
if(Input.GetKeyUp(KeyCode.Space))
{
Instantiate(shott, transform.position, transform.rotation);
}
}
}
Script attached to the ball which gets instantiated:
public class MovementScript : MonoBehaviour {
void Update () {
Rigidbody2D rb;
rb = GetComponent<Rigidbody2D> ();
rb.gravityScale = 0.8f;
transform.Translate( new Vector3(1,1,0) * ShotMoveScript.xForce * Time.deltaTime, Space.Self);
}
}
You rotate the ball when instantiating it with the third parameter rotation (see reference).
After that you apply a Vector pointing to (1, 1) so the ball will move along that direction relative to his local rotation.
Either pass Quaternion.identity as the third parameter to Instantiate method or move the ball along (1, 0) direction.