I am trying to make the basics of a top down game.
I currently have a Player sprite that has a Kinematic RigidBody2D applied to it with a box collider 2D. The scale of this sprite is (1,1).
The wall sprite has a box collider with a scale of (20,1).
To control the player I am using the following code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody2D rb2D;
void Start() {
rb2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate() {
if (Input.GetKey (KeyCode.UpArrow)) {
Vector2 move = new Vector2(0,1);
rb2D.MovePosition(rb2D.position + move);
}
if (Input.GetKey (KeyCode.DownArrow)) {
Vector2 move = new Vector2(0,-1);
rb2D.MovePosition(rb2D.position + move);
}
}
}
With the code above I can control the Player and move them up or down, when attempting to hit the wall below the Player does not hit the wall but instead travels through it as if it was not there.
I'm new to Unity so any other information that might be useful feel free to let me know and I'll update my question.
Thanks!
Uncheck IsKinematic property in the RigidBody2D component. That's because setting isKinematic to true makes your gameObject NOT get affected by any physical or gravity forces.
And also see if your wall is not marked as IsTrigger! Check that in the Collider component of your wall .
Related
So I am developing a game in 2D in Godot and I want to see a line from the center of my sprite to its facing position. I have the sprite rotating and moving along the rotation direction but when I try to create vector out of that its very wrong. For example the vector line is going from the center of the sprite to near (0,0) position on the screen.
public override void _Draw()
{
Vector2 rotationDirection = new Vector2(Mathf.Cos(sprite.GlobalRotation) , Mathf.Sin(sprite.GlobalRotation)) - sprite.GlobalPosition;
DrawLine(sprite.GlobalPosition, rotationDirection, Colors.Red, 2f);
}
EDIT:
Fixed it it works now.
public override void _Draw()
{
DrawLine(sprite.GlobalPosition, sprite.GlobalPosition + new Vector2(Mathf.Cos(sprite.GlobalRotation), Mathf.Sin(sprite.GlobalRotation)) * 50f, Colors.Red, 2f);
}
A simpler solution would have been to add a Line2D child node. You can see the effect right away in the editor, and there's no need for any maths.
This might also be more efficient, because custom _draw functions don't play nice with Godot's geometry batching.
Just need to get some numeric equivalent, which will show how much the wheel is scrolled.
I managed to find an example only using awt/swing: Java Docs
P.S. Sorry for my English.
{
...
sceneChart.setOnScroll(event -> print(event));
}
public void print (ScrollEvent event) {
System.out.println(event.getDeltaY());
}
add onScroll to your javafx element to handle scroll event
for example here add onScroll to ImageView
<ImageView onScroll="#onScrollImageView" .........../>
and handle that in fxml controller like here:
public void onScrollImageView(ScrollEvent scrollEvent) {
double deltaY = scrollEvent.getDeltaY();
if(deltaY<0)
//scroll up
if(deltaY>0)
//scroll down
}
getDeltaY() return the vertical scroll amount.
and negative value of getDeltaY() or positive value means scroll direction
I am trying to create a ground tile that, once is off screen moves to the forward position in front of the other ground tile that is currently in the screen.
I have barely tried anything in code that would be even slightly useful to add to this question. Could someone point me in the right direction to be able to get this done so I don't have to just duplicate hundreds of tiles?
Thanks!
Attach this scrip on your Tileobject.
using UnityEngine;
public class SpawnTiles : MonoBehaviour {
private Material currentTile;
public float speed;
private float offset;
// Use this for initialization
void Start () {
currentTile = GetComponent<Renderer>().material;
}
// Update is called once per frame
void Update () {
offset += 0.001f;
currentTile.SetTextureOffset ("_MainTex", new Vector2 (offset * speed, 0));
}
}
Edit
This is what you want?
If YES, follow theses steps.
Create > 3D Object > Quad (Resize as you want)
Select your tile sprite:
Texture type = texture
Wrap mode = repeat
Now creat new Material: Create > Material
Select: Shader = Unlit/Texture
Texture select your previous texture.
Drag and drop this Material inside Quadobject.
Adjust tiles at inspector.
Result
Creat new scrip SpawnTiles and attach at Quadobject.
using UnityEngine;
public class SpawnTiles : MonoBehaviour {
private Material currentTile;
public float speed;
private float offset;
// Update is called once per frame
void Update () {
GetComponent<Renderer>().material.mainTextureOffset = new Vector2 (Time.time * speed, 0f);
}
}
Ajust tiles movement speed at inspector.
Finish rename Quadobject as you want.
If you want to only move the back tile to the front, you probably don't need it, but I still highly suggest looking into object pooling.
To detect if a camera can't see the tile, use this: Renderer.isVisible
To move the tile, if all the tiles are the same dimensions, you could just increase it's position.x by the amount you need.
I have found one solution using WorldPointToViewport().
Here is my script:
using UnityEngine;
using System.Collections;
public class groundTilingScript : MonoBehaviour {
Transform ground; //For reference to the transform
Camera cam; //Reference to Main Camera
float groundWidth; //The width of the transform, used for calculating current max x position of transform and next placement x position
private float nextXPos = 0.0f; //Store next x position in variable for easier reading
// Use this for initialization
void Start () {
//Set up References
ground = transform;
cam = Camera.main;
//Store Ground width (Width of the ground tile)
groundWidth = ground.GetComponent<Renderer> ().bounds.size.x;
}
// Update is called once per frame
void Update () {
//Create new Vector3 to be used in WorldToViewportPoint so it doesn't use the middle of the ground as reference
Vector3 boxRightPos = new Vector3 (ground.position.x + groundWidth/2, ground.position.y, ground.position.z);
//Store view Position of ground
Vector3 viewPos = cam.WorldToViewportPoint (boxRightPos);
//If the ground tile is left of camera viewport
if (viewPos.x < 0) {
//gameObject is offscreen, destroy it and re-instantiate it at new xPosition
float currentRightX = ground.position.x + groundWidth;
nextXPos = currentRightX + groundWidth;
Instantiate (gameObject, new Vector3 (nextXPos, ground.position.y, ground.position.z), ground.rotation);
Destroy (gameObject);
}
}
}
NOTE: I have to set up two ground tiles initially to give room for camera to go further than the ground tile. (One tile on screen, one off)
Again, this is just my attempt. I am completely open to criticism.
thanks in advance for helping me out.
I'm wrapping up work on a 2D platformer that has a background parallax effect using a texture offset on five different layers. The two cloud layers just have their offset scrolling over time, and the three ground layers scroll based on the camera position. This all works perfectly in Unity and in the PC build and looks great.
However, on the Unity Web Player and webGL builds, instead of the texture wrapping, it looks like this:
I'm new here, so I can't embed images yet it seems
I have no idea what is causing the problem, but instead of wrapping around and acting like a conveyor belt, it just streaks like that. Here is my parallax code:
using UnityEngine;
using System.Collections;
public class sceneryParallax : MonoBehaviour {
[SerializeField]
private Transform cameraTransform;
[SerializeField]
private Renderer renderBGFar;
[SerializeField]
private Renderer renderBGMid;
[SerializeField]
private Renderer renderBGNear;
public float offsetFar;
public float offsetMid;
public float offsetNear;
void Update () {
float xOffset = cameraTransform.position.x;
Vector2 farOffset = new Vector2((xOffset / offsetFar) % 1.0f, 0.0f);
Vector2 midOffset = new Vector2((xOffset / offsetMid) % 1.0f, 0.0f);
Vector2 nearOffset = new Vector2((xOffset / offsetNear) % 1.0f, 0.0f);
renderBGFar.material.mainTextureOffset = farOffset;
renderBGMid.material.mainTextureOffset = midOffset;
renderBGNear.material.mainTextureOffset = nearOffset;
}
}
Also, here is a screen of my texture setup:
Link
Sorry if there are any formatting problems! I really appreciate any help you can offer. If you need more information, let me know!
Thanks!
If your texture wrapping mode is set to clamp, it will reuse the last available pixel. It sounds like texture mode repeat is what you need, opposed to clamping, per the documentation:
This is useful for preventing wrapping artifacts when mapping an image
onto an object and you don't want the texture to tile. UV coordinates
will be clamped to the range 0...1. When UVs are larger than 1 or
smaller than 0, the last pixel at the border will be used. See Also:
Texture.wrapMode, texture assets.
Currently I have a project that is being used to draw rooms with lines and images that can be selected by the user by hitting a button representing what they want to add. I.E. if they want a shower a button for shower is pressed and an image appears in a pane. The shower can be resized and moved in the pane. The user also has the ability to use lines to draw objects or walls. The lines can be resized, rotated, or moved. I am now trying to get these objects to interact with each other. Say a user is using lines to make an object, and when the line comes near another object the line being moved snaps to the other object. I have found a 3rd party library that has SnapLineSnapResult but I don't see anything where someone has used it. Is this something that is desktop JavaFX usable, or is it a touch operation and does anyone have code to model or another solution?
SnapLineSnapResult
My code for line that would be useful if I can use this class is as follows:
line.setOnMouseDragged((MouseEvent event1) -> {
// in resize region
if (isInResizeZoneLine(line, event1)) {
// adjust line
line.setEndX(event1.getX());
}
// in movable region
else {
Point2D currentPointer = new Point2D(event1.getX(), event1.getY());
if (bound.getBoundsInLocal().contains(currentPointer)) {
/*--------*/ // potential place for if (near other object to be snapped)
double lineWidth = line.getEndX() - line.getStartX();
line.setStartY(currentPointer.getY());
line.setEndY(currentPointer.getY());
line.setStartX(currentPointer.getX() - lineWidth/2);
line.setEndX(currentPointer.getX() + lineWidth/2);
}
}
});