So I am very new to unity. and I used this tutorial to create a shooting mechanic for my prototype. moving right and shooting projectiles works fine but when I move left the projectiles are not visible in the game view . I noticed they are in the scene when I run the game. I believe this has nothing to do with my code. since when I press the button it shoots and destroys. it's just not visible when I am moving left. is this a visual issue??
I have screenshots here to provide a better example. of what I'm experiencing. and the links to the movement and projectile code as well for any references. if anyone knows an easy fix it would be much appreciated. If you need to see my code i will include a follow up.
Movement:https://www.youtube.com/watch?v=n4N9VEA2GFo&t=20s[enter image description here]1
Projectile:https://www.youtube.com/watch?v=8TqY6p-PRcs&t=264s
#Nyssa Wootton Welcome to StackOverflow.Try the bullet spawning approach you no need to attach a bullet with the player. Just assign a bullet in inspector and it will spawn a bullet whenever the user clicks on the button. have a look at this code
public GameObject bulletPrefab;
public Transform spawnPos;
public float bulletSpeed = 100f;
void Update () {
if (Input.GetMouseButtonDown(0)){ // on fire btn press
var bullet = Instantiate(bulletPrefab , spawnPos , Quaternion.identity);
// make sure you're prefab have rigidbody on it
bullet.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeed , ForceMode.Impulse);
Destory(bullet , 2); // destorys after 2 second
}
}
Related
I have this script:
Ray ray = new Ray (cam.transform.position, cam.transform.forward);
RaycastHit hit;
Debug.DrawRay (transform.position, ray.direction * 50f);
if (Input.GetKeyDown (KeyCode.E)) {
if (Physics.Raycast (ray, out hit, 50.0f)) {
if (hit.collider.gameObject.tag == "Door") {
Debug.Log ("YEAH");
}
}
}
How I can start animation open door?
There are a few things that you need to know before you can animate that door.
There are multiple ways to animate an object in Unity. For simple things like that you could decide to just rotate the object (but you need to understand that you will need a Coroutine or a Mathf.MoveTowards or Mathf.Lerp method to avoid instant moving when you call everything in the Update).
But you can also use an animation made by someone else in applications like Blender.
Or finally create an animation with the Unity in game editor and create an Animator to animate your door.
I think that you should use this way.
First follow this official manual:
https://docs.unity3d.com/Manual/animeditor-CreatingANewAnimationClip.html
to create the clip to open your door.
It's really intuitive and you don't even need to code.
After that you should create an Animator Controller:
https://docs.unity3d.com/Manual/class-AnimatorController.html
You will than create a new state with your animation and you will create a connection between the initial state to your "Open door" animation.
After that you just create a simple bool (in your Animator Controller).
And you will add
this.GetComponent().SetBool("nameofyourboolintheanimatorcontroller", true);
to your script (of course this is valid only if your Animator Controller is in the object assigned to your script... if not you should create a new Animator variable and assign it (for example you could make it public and assign it in the editor).
I have a problem setting up the camera in my swift 3 project.
and this is my code
cameraNode = childNode(withName: "cameraNode") as! SKCameraNode
view.scene?.camera = cameraNode
Can you help me figure out whats wrong here? Even if i move the camera in my scene editor, it won't make any difference in the view of my game.
Thanks a lot!
Alright mate, got you fam don't worry.
Set a variable the looks something like this,
var GameSceneCamera = SKCameraNode()
Keep your first line of code specifically this,
cameraNode = childNode(withName: "cameraNode") as! SKCameraNode
And delete your second line of code specifically this,
view.scene?.camera = cameraNode
Right, last step mate, this is very important so listen very carefully,
Go to the file GameScene.sks
Open the navigation side bar or Navigator (click on the third button from the top right) to open up your assets on the side
Click on, Scene, or the top asset that contains all of these assets
open the side bar on the right or Utilities (click on the first button from the top right)
Click on camera and select the node
If you can't figure this out you are a special type of stupid lol.
I have my project setup like this:
Main Camera
Physics 2D Raycaster
Event Mask: Everthing
Canvas
Graphic Raycaster
Blocking Objects: None
Blocking Mask: Nothing
2 Objects setup:
GameObject
Sprite Renderer
Rigidbody 2D
Circle Collider 2D
(my GO script)
UI
Image
Button
(my UI script)
In both my GO and UI script, I add OnPointerEnter event. and both work fine on their own. I can receive OnPointerEnter event.
But when I use a joint to drag a GO object and move to top of the UI object. My UI object OnPointerEnter blocked by the GO. I cannot receive the UI OnPointerEnter event.
I search on web and everybody ask for blocking raycast on GO to UI. But I need the reverse, I want both GO and UI receive OnPointerEnter event no matter if they overlap or not. Any hints?
P.S. something like this in 2D version, GameObject block UI Object. But I still want to receive UI Object OnPointerEnter.
Finally get what I want. Now I have 2 solutions:
using OnTriggerEnter2D
turn off Physics2DRaycaster LayerMask GO when dragging GO
1. using OnTriggerEnter2D
When drag GO. Shoot event to UI and tell which GO is dragging.
Add rigibody2D(isKinematic and Freeze xyz) and 2Dcollider(is trigger) as component in UI object.
OnTriggerEnter2D receive my GO collider and check if it is the dragging GO.(doing this because I excatlly want the UI get my only dragging GO).
2. turn off Physics2DRaycaster LayerMask GO when dragging GO
use this code:
using UnityEngine.EventSystems;
public void turnOffLayerMask(string layerMaskName)
{
Physics2DRaycaster p2drc = Camera.main.GetComponent();
LayerMask layerMask = p2drc.eventMask;
LayerMask disableLayerMask = 1 << LayerMask.NameToLayer(layerMaskName);
p2drc.eventMask = layerMask & ~disableLayerMask;
}
public void turnOnLayerMask(string layerMaskName)
{
Physics2DRaycaster p2drc = Camera.main.GetComponent();
LayerMask layerMask = p2drc.eventMask;
LayerMask enableLayerMask = 1 << LayerMask.NameToLayer(layerMaskName);
p2drc.eventMask = layerMask | enableLayerMask;
}
turn off GO layerMask when dragging GO. Turn On back when drag end. The raycast can go through GO to UI and receive OnPointerXXX events.
I think the EventSystem auto. choose EITHER Physical raycast or Graphic raycast to detect GO/UI objects. So you can only receive either one set of event(Non-UI/ UI). Is it correct?
It seems that I search on web and many people using OnMouseXXX (or other method) instead of Event system(i.e. OnPointerXXX). So they can "touch through" GO to UI.
I am able to translate the gameObject from one point to other by the OnMouseDrag() Event have no problem in doing that. But when i have added Animation to the GameObject Using the Animator Im not able to handle its Dynamic translate property when the Drag event take place.
Details:-
The Animation that i have create for the Game object is that it gets Translate from the Outer side of the screen to a side of the screen, This happens on the launch of the scene. (This happens properly)
Now when i want to dynamically translate the game object on the mouse drag it doesn't translate. (if i have not added the animator to the object it translate with mouse drag with out an issue)
Can any Tell me why it is so ?
Thanks in advance.
Did you try making Gameobject with animation,a child of an empty Gameobject and then translating that parent gameobject.
I'm trying to include splash screen into my XNA project while content is loading. I tried few tutorials but none has helped me. I was trying load content in update method, forcing draw method etc. Since I'm doing my project on Windows PC only I'm looking for a solution to display splash screen (propably animated) in the center of desktop. When content is loaded splash screen should disappear and the game screen should pop up.
I'm looking for some advices and tips how do it since I can't find any help on the Internet.
Personally I was thinking about using Forms but I don't know it well to use it properly.
Thanks in advance.
Since you are loading content, you are probably making a method call that spans over many frames. If you make that call on the GUI thread, you will freeze the game, which isn't what you want.
Instead, call the method inside a Task, and set your game's state as 'loading', and when the task is done, set a state that indicates the task is done.
Then, in your drawing method, you can do something like this:
if (state == State.Loading)
// draw loading screen
else if (State == .....
Extending on what Kendall Fray said, You could use game states like so:
public static GameState currentGameState = GameState.Loading;
public enum GameState
{
Loading, Whatever
}
and on Update or draw:
switch (currentGameState)
{
case (GameState.Loading):
//draw load screen
break;
case (GameState.Whatever):
break;
}