I understand that this question has been answered before and I've done my research trust me.
I'll say this now, this is a project for school but what I want to do this for my own learning considering that I will be going in to game programmer.
This might get messy but ill do my best
so for this its started as a basic shoot em up, we have a
game.cs- contains all the textures vectors what not, and the
entity.cs- has already begun spritebatch for player to call a player.draw (spritebatch) in
game.cs
player.cs- contains the shooting of the bullet and the position
bullet.cs
enemy.cs
So my problem is that I have the code for updating the sprite to look at the mouse then I would use
player.Draw(
playerTexture,
position,
null,
Color.White,
rotation,
spriteOrigin,
1.0f,SpriteEffects.None,0f);
In game.cs draw function, but when I do this I get another sprite (same sprite as my player sprite) that does look at my mouse but I want my other one to look at mouse
But my proff. Wrote the code so that there is an already begun spritebatch in entity.cs
public CEntity(CGame myGame, Texture2D myTexture)
{
game = myGame;
texture = myTexture;
}
/// <summary>
/// Draws the player.
/// </summary>
/// <param name="begunSpriteBatch">Give this function a already begun SpriteBatch.</param>
public void Draw(SpriteBatch begunSpriteBatch)
{
begunSpriteBatch.Draw(texture, position, Color.White);
}
So in game.cs draw function player.draw is called to draw the player, what can I do to get that look at mouse effect for that sprite?
public CPlayer(CGame game, Texture2D playerTexture)
: base(game, playerTexture)
{
texture = playerTexture;
brotation = rotation;
position.X = 400f;
position.Y = 400f;
}
And also player is inherited from entity.
Code for updating the mouse as requested
IsMouseVisible = true;
MouseState mouse = Mouse.GetState();
distance.X = mouse.X - position.X;
distance.Y = mouse.Y - position.Y;
//Math.Atan2((double)mouseState.Y - position.Y, (double)mouseState.X - position.X); was using this //before found a different way
rotation = (float)Math.Atan2(distance.Y, distance.X);
spriteRectangle = new Rectangle((int)position.X, (int)position.Y,//rotation stuff
playerTexture.Width, playerTexture.Height);
spriteOrigin = new Vector2(spriteRectangle.Width / 2, spriteRectangle.Height / 2);
Related
I'm using Unity3D with MRTK for Hololens 2 development.
As you can see in the picture I gat a Square-Gameobject and a AppBar-Gameobject. The Square hast MRTK BoundsControl attached to it which make it transformable. The user can change the size and rotation of the Square with the handles.
Here you can see how the scene is looking:
This is the code I'am using to make the AppBar sticky:
public GameObject BasicConfig;
public GameObject RotatingObject;
public Transform TransformObject;
public float Offset;
private bool rotating = false;
private void Update()
{
//Make AppBar sticky to the transformObject
//if (rotating == false)
//{
Vector3 pos = TransformObject.position;
pos.x += Offset;
transform.position = pos;
//}
//Rotate Appbar to match parent
//var rotationVector = RotatingObject.transform.rotation.eulerAngles;
//BasicConfig.transform.rotation = Quaternion.Euler(rotationVector);
}
When I'am rotating the Square with this script the AppBar is looking like this (but it should still look like in the Screenshot above):
As you can see in the commented code I already tried to disable the sticky part in the code when the user is rotating the Square but when I'am scaling the Square afterwards the position is wrong again.
In the commented code part:
//Rotate Appbar to match parent
The AppBar is just rotating around it's own axis.
How can I make the AppBar sticky to the Square and stay in the right Position when I'am rotating the Square?
(in Unity 3D)
Hi.
I want to show "HPBar" of the character on UI Canvas.
I tried the following method, but it didn't work normally.
Please tell me how to solve this problem...
/// <summary>
/// Follow target's position
/// </summary>
/// <param name="target">target(character's world position)</param>
void ChaseCharacter(Vector3 target)
{
// change world position to screen position (Main Camera)
Vector3 p1 = _worldCam.WorldToScreenPoint(target);
// change screen position to world position (UI Camera)
Vector3 p2 = _uiCam.ScreenToWorldPoint(p1);
// Apply UI Position
transform.position = p2;
}
I've built a minimal example, with a single camera. I guess with some small tweaks it should work on your multi-camera case.
public class UIFollow : MonoBehaviour
{
public GameObject target;
public Vector2 offset;
new private Camera camera;
void Start()
{
camera = GetComponentInParent<Canvas>().worldCamera;
}
void Update()
{
((RectTransform)transform).anchoredPosition = camera.WorldToScreenPoint(target.transform.position) + (Vector3)offset;
}
}
UIFollow is a Component on the "HealthBar" object inside the Canvas. Make sure it's anchors are set to the left bottom corner.
The offset vector is used to place the health bar above the object's center, it's public so it's easier to modify while designing the game.
From the hierarchy tab, add a new object, section UI then select Image, a canvas object should appear if not already created, inside should be the white image created , this Image should already follow your camera to every place because its in the UI screen section not near the player, just resize, make transparent and add your health bar inside it.
Thank you all for your advice.
I solved this problem, but I don't know how it was solved.
I'll upload the resolved code.
Vector3 p1 = _worldCam.WorldToScreenPoint(target);
p1.z = 100;
Vector3 p2 = _uiCam.ScreenToWorldPoint(p1);
transform.position = p2;
I am new in Game Programming but I'm interested in this domain; now I am working on a small game for my course work. I have used some ideas from the internet to make my hero jump; the code is working, but after I press first time space, the hero is jumping and is not coming back to his position, he remains on the top of the screen. Please help me to make my hero, then return to his initial position. If I press space again he is jumping, but is jumping there, on the top of the screen.
public void Initialize()
{
startY = position.Y;
jumping = false;
jumpspeed = 0;
}
public void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
AnimateRight(gameTime);//calling AnimateRight function to animate heroes sprite
if (jumping)
{
position.Y += jumpspeed;
jumpspeed += 1;
if (position.Y >= startY)
{
position.Y = startY;
jumping = false;
}
}
else
{
if (keyState.IsKeyDown(Keys.Space))
{
jumping = true;
jumpspeed = -14;
}
}
}
You have to set startY when pressing Space:
if (keyState.IsKeyDown(Keys.Space))
{
jumping = true;
jumpspeed = -14;
startY = position.Y;
}
I know that you are new, but I replicated a movement system that worked great if you can understand it, here is the link to see the player movement in action, and here is the web site for it. If you want to download it here is the link.
This player movement uses a couple important things,
First:
Inside the Player class there is a method called public Player, yes, you make a method that is the same name as the class. By doing this you can transfer information from the Game1 class to Player class. So you can send the player texture, position, speed, ect...
Second:
Inside the Player method the info that is called over from the Game1 class needs to be collected and stored in the Player class. So if you wanted to transfer the texture of you player you would need to do the following.
Create the Player Texture and create a the link that will allow you to create the link to the Player class:
Texture2D personTexture;
'Player player'
Then in the Load Content you need to call on the personTexture and put it into the player function:
personTexture = Content.Load <Texture2D>("Person");
player = new Player(personTexture);
Now that the Texture is now in side the Player method in the Player class you will now store it in the Player class so that you can use it, add a Texture2D Texture to your Player class then enter the following:
public Player(Texture2D Texture)
{
this.Texture = Texture;//this.Texture is the one you create in side the
Player class, the other is the Texture you stated
}
Now your done and able to use your texture in that class.
Hopefully this will help you understand how to create your jumping player.
I am a newbie in game development, and I am developing a concept for windows store using monogame implementation of XNA where in, I am making a ball move on flick gesture. But, my problem is the ball moves not only on applying the gesture on to the ball but applying the gesture anywhere on the screen makes my ball move. What I want is, my ball shall only move when I apply flick on the ball itself but not on the other areas of the screen. I am using rectangle bounding box to see whether user touches the ball object but in vain.
I am sharing with my method to Update Gametime
//Global vars
//cat is the ball object here
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
private Texture2D cat;
private Vector2 _spritePosition;
private SpriteFont _fontMiramonte;
BounceableImage mBall;
const float DECELERATION = 1000;
Vector2 position = Vector2.Zero;
Vector2 velocity;
//Update method
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (IsPointInObject(position))
{
if (gesture.GestureType == GestureType.Flick)
velocity += gesture.Delta;
if (gesture.GestureType == GestureType.Hold)
position = gesture.Position;
}
}
// Use velocity to adjust position and decelerate
if (velocity != Vector2.Zero)
{
float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
position += velocity * elapsedSeconds;
float newMagnitude = velocity.Length() - DECELERATION * elapsedSeconds;
velocity.Normalize();
velocity *= Math.Max(0, newMagnitude);
}
UpdateSprite(gameTime, ref position, ref velocity);
base.Update(gameTime);
}
//method to detect whether the touched point lies inside the object ball
public bool IsPointInObject(Vector2 positionVector)
{
Rectangle bbx = new Rectangle((int)position.X , (int)position.Y, (int)cat.Width, (int)cat.Height);
return bbx.Contains((int)(positionVector.X), (int)(positionVector.Y));
}
You'll have to set a canvas on TouchPanel and set and lock this canvas for gesture movements from player. You'll have to set canvas block smaller from Panel, where you want to see touch effect.
Trying to make a 2D side scroller here
I am quite new to programming. I've tried to follow guides and tutorials with not much luck. I am aware that this is quite simple but i just cannot figure it out.
I have multiple classes for all the different characters in the game.
I have a rectangle for the main sprite character which the player will control.
But the problem is that I want to add rectangles around enemy sprites so that i can add collision into the game.
public class enemyRocks
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public Rectangle rockRectangle;
public bool isVisible = true;
Random random = new Random();
int randX;
public enemyRocks(Texture2D newTexture, Vector2 newPosition)
{
texture = newTexture;
position = newPosition;
randX = -5;
velocity = new Vector2(randX, 0);
}
public void Update(GraphicsDevice graphics)
{
position += velocity;
if (position.X < 0 - texture.Width)
isVisible = false;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
I have genuinely tried many methods but it just doesn't seem to work.
Everything i've done so far gave me a "nullreferenceexception was unhandled" error.
I will take any criticism needed to improve.
Thank you for your help.
you need boundingBox property on your sprites
new Rectangle((int)Position.X,(int)Position.Y,texture.Width, texture.Height);
then to check collision
if (Sprite1.BoundingBox.Intersects(Sprite2.BoundingBox))
but make sure that you load your texture before any function that uses texture. i guess your error happened on update function where you try to get width of texture that is not loaded.