XNA 3.1 Movement Collision Issue - c#

So heres my problem. I have a box that I want my character to move around. But I want to be able to move around it while holding multiple move commands, for instance..
when moving right (towards the left of the obstacle) I want to be able to hold move right and up or down at the same time without the character sticking to the box. The funny part is, it works fine for the left and right side of the obstacle, yet it sticks when i try it on the top and bottom side of the obstacle.
Heres the Player Class (object im moving)
<!-- language: c# -->
public class Player
{
public Texture2D texture;
public Vector2 position;
public int speed;
public Vector2 offset;
public bool left, right, up, down;
public Rectangle collisionRect
{
get
{
return new Rectangle((int)position.X , (int)position.Y, texture.Width, texture.Height);
}
}
public Vector2 direction;
public Player(Texture2D texture, Vector2 position, int speed)
{
this.texture = texture;
this.position = position;
this.speed = speed;
offset.X = speed;
offset.Y = speed;
left = false;
right = false;
up = false;
down = false;
}
public virtual void Update(GameTime gameTime, Rectangle clientBounds)
{
direction = Vector2.Zero;
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
direction.X -= 1;
left = true;
}
else
left = false;
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
direction.X += 1;
right = true;
}
else
right = false;
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
direction.Y -= 1;
up = true;
}
else
up = false;
if (Keyboard.GetState().IsKeyDown(Keys.S))
{
direction.Y += 1;
down = true;
}
else
down = false;
position += (direction * speed);
}
public virtual void Draw(GameTime gameTime, SpriteBatch spritebatch)
{
spritebatch.Draw(texture, collisionRect, Color.White);
}
}
Heres the Update Method in my maingame
<!-- language: c# -->
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
player.Update(gameTime, Game.Window.ClientBounds);
if (player.right && HitWall(player))
{
player.position.X -= player.offset.X;
}
else if (player.left && HitWall(player))
{
player.position.X += player.offset.X;
}
if (player.down && HitWall(player))
{
player.position.Y -= player.offset.Y;
}
else if (player.up && HitWall(player))
{
player.position.Y += player.offset.Y;
}
base.Update(gameTime);
}
And the HitWall function
<!-- language: c# -->
public bool HitWall(Player player)
{
for (int i = player.collisionRect.Top; i < player.collisionRect.Bottom; i++)
for (int j = player.collisionRect.Left; j < player.collisionRect.Right; j++)
if (TextureData[i * gameMap.map.Width + j] != Color.White)
return true;
return false;
}

I'm not sure where offset is defined, but I'm assuming it's the movement you've just made that frame.
Your problem is that because you check left & right before up and down, if you're moving diagonally down onto the top edge of the box, then you'll register a hit in the Y direction — HitWall doesn't check which direction you're going, it just checks for a collision. Therefore, the collision in the Y axis stil counts on the line if (player.right && HitWall(player)) and stops your lateral movement.
Best bet is to apply your sideways movement, check for a hit, move back if there is one — then apply your downwards movement, check for a hit, and move back if there is one. Correcting the position like this should mean you slide along the sides as you want.

Related

How can I rotate the capsule only on the Y?

RotateTo is call from the Update. I want it to rotate on the Y only.
now it seems like it's rotating on all axis X,Y,Z
private void RotateTo()
{
var distance = Vector3.Distance(capsule.position, curvedLinePoints[rotationIndex].transform.position);
if(distance < 0.1f)
{
rotationIndex++;
}
// Determine which direction to rotate towards
Vector3 targetDirection = curvedLinePoints[rotationIndex].transform.position -capsule.position;
// The step size is equal to speed times frame time.
float singleStep = rotationSpeed * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(capsule.forward, targetDirection, singleStep, 0.0f);
// Calculate a rotation a step closer to the target and applies rotation to this object
capsule.rotation = Quaternion.LookRotation(newDirection);
}
This is the full script.
The transform is moving along g waypoints there are more than 4000 waypoints the capsule should only rotate looking each time to the next curvedLinePoint from the List curvedLinePoints.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class WaypointsFollower : MonoBehaviour
{
public float speed;
public Waypoints waypoints;
public Transform capsule;
public bool go;
public bool goForward;
public float rotationSpeed;
private int index = 0;
private int rotationIndex = 0;
private int counter = 0;
private int c = 0;
private List<GameObject> curvedLinePoints = new List<GameObject>();
public int numofposbetweenpoints;
private bool getonce;
private bool getBackwardIndexOnce = true;
private void Start()
{
waypoints = GameObject.Find("Waypoints").GetComponent<Waypoints>();
curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
if(waypoints.moveInReverse == false)
{
goForward = true;
}
else
{
goForward = false;
}
if(goForward)
{
index = 0;
}
}
private void Update()
{
if (getonce == false)
{
numofposbetweenpoints = curvedLinePoints.Count;
getonce = true;
}
if (go == true && waypoints.lineRendererPositions.Count > 0)
{
if(goForward == false && getBackwardIndexOnce)
{
index = waypoints.lineRendererPositions.Count - 1;
getBackwardIndexOnce = false;
}
RotateTo();
Move();
}
}
private void Move()
{
Vector3 newPos = transform.position;
float distanceToTravel = speed * Time.deltaTime;
bool stillTraveling = true;
while (stillTraveling)
{
Vector3 oldPos = newPos;
// error exception out of bound on line 55 to check !!!!!
newPos = Vector3.MoveTowards(oldPos, waypoints.lineRendererPositions[index], distanceToTravel);
distanceToTravel -= Vector3.Distance(newPos, oldPos);
if (newPos == waypoints.lineRendererPositions[index]) // Vector3 comparison is approximate so this is ok
{
// when you hit a waypoint:
if (goForward)
{
bool atLastOne = index >= waypoints.lineRendererPositions.Count - 1;
if (!atLastOne)
{
index++;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index--; goForward = false; }
}
else
{ // going backwards:
bool atFirstOne = index <= 0;
if (!atFirstOne)
{
index--;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index++; goForward = true; }
}
}
else
{
stillTraveling = false;
}
}
transform.position = newPos;
}
private void RotateTo()
{
var distance = Vector3.Distance(capsule.position, curvedLinePoints[rotationIndex].transform.position);
if(distance < 0.1f)
{
rotationIndex++;
}
// Determine which direction to rotate towards
Vector3 targetDirection = curvedLinePoints[rotationIndex].transform.position -capsule.position;
// The step size is equal to speed times frame time.
float singleStep = rotationSpeed * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(capsule.forward, targetDirection, singleStep, 0.0f);
// Calculate a rotation a step closer to the target and applies rotation to this object
capsule.rotation = Quaternion.LookRotation(newDirection);
}
}
This is a screenshot of the hierarchy the platform is the moving transform in the script and the capsule is the capsule that should only rotate facing each time the next curvedLinePoint. There are over 4000 waypoints but only 10 curvedLinePoints. a bit confusing. The capsule should rotate facing each time the next curvedLinePoint while the transform is moving along the waypoints.
Screenshot
I tried this now :
private void RotateTo()
{
var distance = Vector3.Distance(capsule.position, curvedLinePoints[rotationIndex].transform.position);
if(distance < 0.1f)
{
rotationIndex++;
}
var lookPos = curvedLinePoints[rotationIndex].transform.position - capsule.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
capsule.rotation = Quaternion.Slerp(capsule.rotation, rotation, Time.deltaTime * rotationSpeed);
}
and it's rotating bu when I'm looking on the capsule I don't see any of the axis pointing the next point for example in this screenshot the blue is facing to the right the green up the red more ore less down while the point it's moving to is on the left cube so the capsule rotates but why none of the axis is pointing at its direction?
Rotation
I also added eyes to the capsule on the blue axis thinking the blue axis is forward but the capsule is rotating the eyes are never facing the next target.
This screenshot is just to show the eyes on the blue axis direction.
Eyes

How to fix movement with swipe to right and left?

I lost a lot of time trying to find what is the problem in code but I can not find the solution why my code is not triggered.
In my previous game when I implemented this code it worked perfectly, now when i implement into new game this same code for touch movement it doesn't work.
I tried to debug the code and put Debug.Log into Update method and when i swipe over screen it doesn't even get trigger.
This is the code:
int left = 0;
int right = 0;
int maxLeftCycles = 5;
int maxRightCycles = 5;
void Start()
{
//touch
left = maxLeftCycles;
right = maxRightCycles;
}
private void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyUp(KeyCode.RightArrow) ||
Swipe.swipe == Swipe.SwipeDirection.right)
{
Swipe.ResetSwipe();
right = 0;
}
if (Input.GetKeyUp(KeyCode.LeftArrow) ||
Swipe.swipe == Swipe.SwipeDirection.left)
{
Swipe.ResetSwipe();
left = 0;
}
}
void FixedUpdate()
{
if (left < maxLeftCycles && !isMoving)
{
desiredPos = transform.position + Vector3.left * 1.52f;
isMoving = true;
left++;
}
if (right < maxRightCycles && !isMoving)
{
desiredPos = transform.position - Vector3.right * 1.52f;
isMoving = true;
right++;
}
if (isMoving)
{
transform.position = Vector3.MoveTowards(transform.position, desiredPos, moveSpeed * Time.deltaTime);
// this == is true if the difference between both
// vectors is smaller than 0.00001
if (transform.position == desiredPos)
{
isMoving = false;
transform.position = desiredPos;
}
}
}
I put Debug.Log in this code and in vector3.right and left but it never get triggered.
if (Input.GetKeyUp(KeyCode.RightArrow) ||
Swipe.swipe == Swipe.SwipeDirection.right)
{
Debug.Log("This is traacked");
Swipe.ResetSwipe();
right = 0;
}
if (Input.GetKeyUp(KeyCode.LeftArrow) ||
Swipe.swipe == Swipe.SwipeDirection.left)
{
Debug.Log("This is traacked");
Swipe.ResetSwipe();
left = 0;
}
This is the code for Swipe script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swipe : MonoBehaviour
{
private float fingerStartTime = 0.0f;
private Vector2 fingerStartPos = Vector2.zero;
private bool isSwipe = false;
private float minSwipeDist = 50.0f;
private float maxSwipeTime = 0.5f;
public enum SwipeDirection
{
none,
up,
down,
right,
left
}
public static SwipeDirection swipe;
void Start()
{
swipe = SwipeDirection.none;
}
public static void ResetSwipe()
{
swipe = SwipeDirection.none;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began:
/* this is a new touch */
isSwipe = true;
fingerStartTime = Time.time;
fingerStartPos = touch.position;
break;
case TouchPhase.Canceled:
/* The touch is being canceled */
isSwipe = false;
break;
case TouchPhase.Ended:
float gestureTime = Time.time - fingerStartTime;
float gestureDist = (touch.position - fingerStartPos).magnitude;
if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
{
Vector2 direction = touch.position - fingerStartPos;
Vector2 swipeType = Vector2.zero;
if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y))
{
// the swipe is horizontal:
swipeType = Vector2.right * Mathf.Sign(direction.x);
}
else
{
// the swipe is vertical:
swipeType = Vector2.up * Mathf.Sign(direction.y);
}
if (swipeType.x != 0.0f)
{
if (swipeType.x > 0.0f)
{
// MOVE RIGHT
swipe = SwipeDirection.right;
}
else
{
// MOVE LEFT
swipe = SwipeDirection.left;
}
}
if (swipeType.y != 0.0f)
{
if (swipeType.y > 0.0f)
{
// MOVE UP
swipe = SwipeDirection.up;
}
else
{
// MOVE DOWN
swipe = SwipeDirection.down;
}
}
}
break;
}
}
}
}
}
The code in Update method for swipe input which I debug never get called or never work for me.I can not understand what i am doing wrong because the same code actually works in my previous game.
Thank you so much for reading my question I hope there will be some guy who can help me to solve this issue.
If it worked as is before hand in another project I would make sure that you are attaching the script to a game object. If it is attached to a game object make sure that it is not marked as inactive and that you aren't turning the object to inactive somewhere else in your scripts.
If neither of these are the case I would also try removing the script from the object and then reattaching it, and if that still doesn't work try deleting the object the script is attached to (if possible) and then recreate it and reattach the script.

I cannot get my player to move twice in a row

Sorry if the question seems vague but I am creating a turn based, 2D roguelike game for coding practice and want the player to be able to move twice while the enemy moves once when the user clicks a button. I have tried using multiple booleans but to no avail.
The players turn is an integer which once it reaches 0, it becomes the enemies turn. The code for when the user clicks the button:
public void ButtonClicked()
{
GameManager.instance.playersTurn = 2;
}
the movement code inside my player manager(this is where the error lies)
void Update () {
if (0 > GameManager.instance.playersTurn)
return;
horizontal = (int)Input.GetAxisRaw ("Horizontal");
vertical = (int)Input.GetAxisRaw ("Vertical");
//dont allow vertical movement
if (horizontal != 0)
vertical = 0;
//if a key is pressed:
if (vertical != 0 || horizontal != 0)
AttemptMove<Wall> (horizontal, vertical);
}
protected override void AttemptMove <T>(int xDir, int yDir)
{
food--;
foodText.text = "Food: " + food;
base.AttemptMove<T> (xDir, yDir);
CheckIfGameOver ();
GameManager.instance.playersTurn--;
}
the GameManager.instance.playersTurn--; decrements until zero even if I have only moved once. How can I decrement it once per movement? I think the problem is that this is being called in update. Any pointers will be a great help, thanks guys.
Here is the other code from my parent class in case the error might lie here:
protected bool Move(int xDir, int yDir, out RaycastHit2D hit)
{
Vector2 start = transform.position;
Vector2 end = start + new Vector2 (xDir, yDir);
boxCollider.enabled = false;
hit = Physics2D.Linecast (start, end, blockingLayer);
boxCollider.enabled= true;
if (hit.transform == null) {
StartCoroutine (SmoothMovement (end));
return true;
}
return false;
}
protected virtual void AttemptMove<T> (int xDir, int yDir)
where T: Component
{
RaycastHit2D hit;
bool canMove= Move (xDir, yDir, out hit);
if (hit.transform == null)
return;
T hitComponent = hit.transform.GetComponent<T> ();
if (!canMove && hitComponent != null)
OnCantMove (hitComponent);
}
protected IEnumerator SmoothMovement(Vector3 end)
{
float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
while (sqrRemainingDistance > float.Epsilon) {
Vector3 newPosition = Vector3.MoveTowards (rb2d.position, end, inverseMoveTime * Time.deltaTime);
rb2d.MovePosition (newPosition);
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
yield return null;
}
}

How to draw a sprite on a moving canvas

I generally figure this sort of thing out normally but I am stumped. I suspect theres a mathematical combination I have missed but anyway.
I have a moving background (currently goes up and down from top to bottom)
I have a moving object (currently moves left and right from the centre of the canvas programatically).
So this is the question, How can I make an object move relatively to the position on the canvas in x and y directions?
Here is my relevant codes:
//Helper method
private Vector2 CalculateDirection()
{
Vector2 calculatedDirection = new Vector2((float)Math.Cos(direction),
(float)Math.Sin(direction));
calculatedDirection.Normalize();
return calculatedDirection;
}
object on canvas
public void Update(GameTime gameTime, Vector2 center)
{
this.currentCentre = originalCentre - center;
//movement logic here
Vector2 calculatedDirection = CalculateDirection();
//deltaTime = ((float)gameTime.ElapsedGameTime.TotalMilliseconds) / 15f;
if (speed > 0f || speed < 0f)
{
///TODO: work this out!!
Velocity = calculatedDirection * speed;
float dir = (originalCentre.Y - currentCentre.Y);
position.X += Velocity.X * (1.0f - 0.9f);
position.Y = dir;// *(1.0f - 0.9f);
}
}
canvas moving methods
private void determinePitchSize()
{
int newHeight = Convert.ToInt32(pitch.Height * ratio);
this.canvas = new Rectangle(
0, posHeight,
device.PresentationParameters.BackBufferWidth,
newHeight
);
}
public void increasePosHeight()
{
posHeight++;
}
public void decreasePosHeight()
{
posHeight--;
}
private void determineDirection()
{
if (!direction)
{
if (this.canvas.Height + this.canvas.Y <= this.screenY)
direction = true;
}
else
{
if (this.canvas.Y >= 0)
direction = false;
}
}
private void useDirection()
{
this.determineDirection();
if (direction)
this.increasePosHeight();
else decreasePosHeight();
}
If you need any more info I can add it here.
Thanks
Ok so thanks to Nico, I was able to answer this.
Vector2 Velocity { get; set; }
Vector2 relative { get; set; }
public void Update(GameTime gameTime, Vector2 center)
{
this.currentCentre = center;
Vector2 calculatedDirection = CalculateDirection();
if (speed > 0f || speed < 0f)
{
Velocity = calculatedDirection * speed * 0.1f;
relative = relative - Velocity;
position = currentCentre + relative;
}
}
The velocity creates object movement to test that it ends up in a different place.
Relative starts at 0,0 (the center) and is adjusted by the velocity.
Position is then set to the centre plus the relative position. which has been set by the velocity.

Jumping animation

stayI need to make an animation while my sprite is jumping. I tried to load an animation if a key is pressed but it doesn't function. I jump by pressing Space key. All others animations function. So, my problem is to load an animation by pressing a key. I think I could use GameTime.Elapsed.TotalMilliseconds but I am not sure. I am new anyway and it is my first game.
class Player
{
PlayerAnimation animation;
Animation walk;
Animation stay;
Animation jump;;
public Vector2 position;
public Vector2 velocity;
public Rectangle rect;
public bool jumping = false;
public int health = 5;
public Vector2 Position{ get { return position; } }
public Player() { }
public void Load(ContentManager Content) {
walk = new Animation(Content.Load<Texture2D>("walk"), 46, 0.1f, true, rect);
animazioneMinima = new Animation(Content.Load<Texture2D>("stay"),37, 0.15f, true, rect);
jump = new Animation(Content.Load<Texture2D>("jump"), 51, 0.1f, true , rect);
}
public void Update(GameTime gameTime)
{
position += velocity;
rect = new Rectangle((int)position.X, (int)position.Y, 43, 46);
Input(gameTime);
if (velocity.X != 0)
PlayerAnimation.PlayAnimation(walk);
else if (velocity.X == 0)
PlayerAnimation.PlayAnimation(stay;
if (velocity.Y < 10)
velocity.Y += 0.4f;
}
private void Input(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.D))
velocity.X = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 3;
else if (Keyboard.GetState().IsKeyDown(Keys.A))
velocity.X = -(float)gameTime.ElapsedGameTime.TotalMilliseconds / 3;
else velocity.X = 0f;
if (Keyboard.GetState().IsKeyDown(Keys.Space) && jumping == false)
{
position.Y -= 5f;
velocity.Y = -9f;
jumping = true;
}
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
SpriteEffects rotate = SpriteEffects.None;
if (velocity.X >= 0)
rotate = SpriteEffects.None;
else if (velocity.X < 0)
rotate = SpriteEffects.FlipHorizontally;
PlayerAnimation.Draw(gameTime, spriteBatch, position, rotate);
}
}
}
what should i make? :D
I don't know exactly how your code works, but maybe yau can write something like:
if (velocity.Y != 0)
{
PlayerAnimation.PlayAnimation(jump);
velocity.Y -= 0.05f
}
and insted of:
if (Keyboard.GetState().IsKeyDown(Keys.Space) && jumping == false)`
you can write:
if (Keyboard.GetState().IsKeyDown(Keys.Space) && velocity.Y == 0)
then you can skip using the jump bool completely.

Categories