i'm moving game object with this simple function which move body depends on camera view. The only problem that the camera X rotation isnt 0 and body tries to move into the ground but not clear forward and it makes movement slowly
void VerySimpleMove()
{
if (_controllerBody.isGrounded)
{
_moveDirection.x = _mj.GetAxis("Horizontal");
_moveDirection.y = 0;
_moveDirection.z = _mj.GetAxis("Vertical");
_moveDirection = _camera.TransformDirection(_moveDirection);
if (Mathf.Abs(_moveDirection.x) > 0 || Mathf.Abs(_moveDirection.y) > 0)
{
_body.rotation = Quaternion.LookRotation(_moveDirection);
}
if (_jumpButton)
{
_jumpButton = false;
_moveDirection.y = _jumpHeight;
}
}
_moveDirection.y -= _gravity * Time.deltaTime;
_controllerBody.Move(_moveDirection * Time.deltaTime);
}
How is it possible to rotate _moveDirection by _camera.eulerAngle.x to make it real forward?
If I understand correctly, you need to make sure that _moveDirection is always parallel to XZ plane.
One way to do it is instead of using _camera.TransformDirection, to calculate the projection of camera forward vector on XZ plane, then use LookRotation in that direction, like this:
var forward = _camera.transform.forward; // Get camera forward vector
forward.y = 0; // Project it on XZ plane
_moveDirection = Quaternion.LookRotation(forward) * _moveDirection; // Rotate
Note that simply rotating the object by camera X angle as it was suggested won't always help here, since camera Z angle can cause the same effect.
So final code would be:
void VerySimpleMove()
{
if (_controllerBody.isGrounded)
{
_moveDirection.x = _mj.GetAxis("Horizontal");
_moveDirection.y = 0;
_moveDirection.z = _mj.GetAxis("Vertical");
var forward = _camera.transform.forward; // Get camera forward vector
forward.y = 0; // Project it on XZ plane
_moveDirection = Quaternion.LookRotation(forward) * _moveDirection; // Rotate
if (Mathf.Abs(_moveDirection.x) > 0 || Mathf.Abs(_moveDirection.y) > 0)
{
_body.rotation = Quaternion.LookRotation(_moveDirection);
}
if (_jumpButton)
{
_jumpButton = false;
_moveDirection.y = _jumpHeight;
}
}
_moveDirection.y -= _gravity * Time.deltaTime;
_controllerBody.Move(_moveDirection * Time.deltaTime);
}
Related
I am trying to make my camera smoothly zoom in and out whenever I use a mouse scroll wheel, but for some reason it zooms instantly and not smoothly.
This is how I update the zoom:
[SerializeField, Range(10, 100)] float scrollSpeed = 10f;
[SerializeField] Vector2 zoomAmount = Vector2.zero;
private float ScrollWheel
{
get { return Input.GetAxis("Mouse ScrollWheel"); }
}
private new Camera camera = null;
void Update()
{
if (camera == null) return;
UpdateZoom();
}
void UpdateZoom()
{
pos = camera.transform.position;
pos = Vector3.Lerp(pos, new Vector3(pos.x, pos.y - scrollSpeed * 10 * ScrollWheel, pos.z), Time.deltaTime * 2);
pos.y = Mathf.Clamp(pos.y, zoomAmount.x, zoomAmount.y);
camera.transform.position = pos;
}
I think you're a little confused about Lerp (I was confused in the same way too when I started using it). When you pass in a 0 for the time argument (the third argument), it will return your "starting" vector. When you pass a value of 1 or more for the time argument, it will return your "ending" vector. However, if you're passing Time.deltaTime * 2, then you'll be returning approximately the same interpolated vector every frame. Lerp doesn't "track" how far it's already interpolated, so by passing in the same time value every frame, Lerp will never actually return your ending vector. So rather than passing Time.deltaTime * 2, you'd need to do something like this
float interpolatedTime = 0;
void Update()
{
var myVector = Vector3.Lerp(vector1, vector2, this.interpolatedTime);
this.interpolatedTime += Time.deltaTime;
}
How about something like this for your camera:
float zoomTime;
float zoomTarget;
float lastScrollWheelDirection;
void Update()
{
// If this camera is currently zooming in and the player started zooming
// out (or vice versa), reset the amount that is remaining to be zoomed
if ((this.lastScrollWheelDirection > 0 && this.ScrollWheel < 0) ||
(this.lastScrollWheelDirection < 0 && this.ScrollWheel > 0))
{
this.zoomTarget = 0;
}
if (this.ScrollWheel != 0)
{
this.lastScrollWheelDirection = this.ScrollWheel;
}
// zoomTarget is the total distance that is remaining to be zoomed.
// Each frame that the scroll wheel is moved, we'll add a little more
// to the distance that we want to zoom
zoomTarget += this.ScrollWheel * this.scrollSpeed;
// zoomTime is used to do linear interpolation to create a smooth zoom.
// Each time the player moves the mouse wheel, we reset zoomTime so that
// we restart our linear interpolation
if (this.ScrollWheel != 0)
{
this.zoomTime = 0;
}
if (this.zoomTarget != 0)
{
this.zoomTime += Time.deltaTime;
// Calculate how much our camera will be moved this frame using linear
// interpolation. You can adjust how fast the camera zooms by
// changing the divisor for zoomTime
var translation = Vector3.Lerp(
new Vector3(0, 0, 0),
new Vector3(0, this.zoomTarget, 0),
zoomTime / 4f); // see comment above
// Zoom the camera by the amount that we calculated for this frame
this.transform.position -= translation;
// Decrease the amount that's remaining to be zoomed by the amount
// that we zoomed this frame
this.zoomTarget -= translation.y;
}
}
I am working on a simple patrol script, and everything works but the characters are randomly rolling through the floor when they turn around. Here is a short clip of them...
Here is my script...
public class Patrol : MonoBehaviour
{
public float speed = 5;
public float directionChangeInterval = 1;
public float maxHeadingChange = 30;
public bool useRootMotion;
CharacterController controller;
float heading;
Vector3 targetRotation;
Vector3 forward
{
get { return transform.TransformDirection(Vector3.forward); }
}
void Awake()
{
controller = GetComponent<CharacterController>();
// Set random initial rotation
heading = Random.Range(0, 360);
transform.eulerAngles = new Vector3(0, heading, 0);
StartCoroutine(NewHeadingRoutine());
}
void Update()
{
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
if (useRootMotion)
{
return;
}
else
{
controller.SimpleMove(forward * speed);
}
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "Player")
{
// Bounce off the obstacle and change direction
var newDirection = Vector3.Reflect(forward, hit.normal);
transform.rotation = Quaternion.FromToRotation(Vector3.forward, newDirection);
heading = transform.eulerAngles.y;
NewHeading();
}
if (hit.gameObject.tag == "Boundary")
{
// Bounce off the obstacle and change direction
var newDirection = Vector3.Reflect(forward, hit.normal);
transform.rotation = Quaternion.FromToRotation(Vector3.forward, newDirection);
heading = transform.eulerAngles.y;
NewHeading();
}
}
/// Finds a new direction to move towards.
void NewHeading()
{
var floor = transform.eulerAngles.y - maxHeadingChange;
var ceil = transform.eulerAngles.y + maxHeadingChange;
heading = Random.Range(floor, ceil);
targetRotation = new Vector3(0, heading, 0);
}
/// Repeatedly calculates a new direction to move towards.
IEnumerator NewHeadingRoutine()
{
while (true)
{
NewHeading();
yield return new WaitForSeconds(directionChangeInterval);
}
}
}
I have tried adding a rigidbody to the characters and constraining rotation, but that doesnt work. Oddly enough, the character control isnt rotating at all. In the scene view I can see the character collider staying as it should, but the character flips through the mesh on its own.
It looks like it's because they are walking into a corner and being bounced between the two walls constantly which causes them to behave strangely. I would add a method of checking for a series of very quick collisions to detect that they are in a corner or stuck and then adapt accordingly, perhaps with a method to rotate 180 degrees and keep walking or the like.
You can do it like this:
float fTime = 0.1f
float fTimer = 0;
int iCollisionCounter;
if(collision){
if(fTimer > 0) iCollisionCounter++;
if(iCollisionCounter >= 5) //Character is stuck
fTimer = fTime;
}
Void Update(){
fTimer -= time.deltaTime;
}
That means that if there are multiple collisions within 0.1 seconds of each other you can handle it.
Hope this helps!
I am using a translation matrix to move the screen but when the player collides with an object the player will jitter as if it wants to be in 2 places at once. It looks like velocity wants to keep going down while the block pushes it up, how would I go by fixing this?
Video: Here
Camera class:
class Camera
{
public Vector2 Position;
Viewport viewPort;
public Vector2 cameraBounds;
public float wasGround;
public Matrix Transform()
{
var translationMatrix = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));
return translationMatrix;
}
public Player thing(Player player)
{
cameraBounds.X = player.Position.X - Game1.offset.X;
if (cameraBounds.X > 0)
Position.X = player.Position.X - Game1.offset.X;
else
Position.X = 0;
//Problem
cameraBounds.Y = player.Position.Y - Game1.offset.Y;
if (cameraBounds.Y > 0)
{
Position.Y = player.Position.Y - Game1.offset.Y;
if (player.goingUp == false && (wasGround != player.ground))
Position.Y = player.ground - Game1.offset.Y;
wasGround = player.ground;
}
else
Position.Y = 0;
return player;
}
public Camera(Viewport viewport)
{
viewPort = viewport;
}
}
I tried to fix the problem by adding in player goingUp and ground if statements but that did not help.
I solved it. It is about sequence of operations. Just move method camera.thing() as shown below:
// TODO: Add your update logic here
HandleInput(Keyboard.GetState());
player.Update(gameTime);
// delete from here
Time += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach (Block b in Blocks)
{
player = b.BlockCollision(player);
}
// place here
camera.thing(player);
Explanation: You have to set camera position after all collisions have done.
what i can see while you stand still on some object, velocity changes. so try to convert camera position to integer. or make velocity exact 0 if is near 0.
cameraBounds.X = cInt(cameraBounds.X)
cameraBounds.y = cInt(cameraBounds.y)
I'm wanting my arrow to point the way I'm targeting in my game.
At the moment, my prefab instantiates in the center of the screen point to the left. When I move my arrow, the point still keeps to the left. How can I get it so that the arrow at least points to the center of my screen, or ideally, has a way of moving, depending on the direction the player is aiming.
For example, if my player is aiming to the top left of the screen, the arrow point would face that way and fire off in that direction.
Here is the code i have so far:
public Object _projectilePrefab;
private bool _pressed = false;
public GameObject _flystickPosition;
private GameObject _currentProjectile;
private Vector3 _firePoint;
private float _startTime;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
int x = 0;
int y = 0;
// Use this for initialization
void Start()
{
_currentProjectile = (GameObject)Instantiate(_projectilePrefab, _flystickPosition.transform.position, _flystickPosition.transform.rotation);
_firePoint = Camera.mainCamera.transform.position - new Vector3(1, 1, 0);
_startTime = (Time.time * 10.0f);
//_serialData = GetComponent<ReadSerial>();
//_udpData = GetComponent<HV_ReadUDPData>();
}
// Update is called once per frame
void Update()
{
_firePoint = Camera.mainCamera.transform.position - new Vector3(1, 1, 0);
float _moveBananaY = transform.localEulerAngles.y + Input.GetAxisRaw("LeftRight") * sensitivityX;
float _moveBananaX = transform.localEulerAngles.x + Input.GetAxisRaw("UpDown") * sensitivityY;
transform.localEulerAngles = new Vector3(_moveBananaX, _moveBananaY, 0);
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
y--;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
y++;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
x--;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
x++;
}
float xPercent = 0;
float yPercent = 0;
xPercent = x / 25.0f;
yPercent = y / 25.0f;
_flystickPosition.transform.parent.localEulerAngles = new Vector3(xPercent * 90, yPercent * 90, 0.0f);
float smooth = Time.time - _startTime;
_currentProjectile.transform.position = Vector3.Lerp(lerpFrom, _flystickPosition.transform.position, smooth);
if (Input.GetKeyDown(KeyCode.Space))
{
_currentProjectile.GetComponent<Rigidbody>().velocity = _flystickPosition.transform.parent.forward.normalized * 20;
_currentProjectile = null;
_currentProjectile = (GameObject)Instantiate(_projectilePrefab, _firePoint, transform.rotation);
lerpFrom = _currentProjectile.transform.position;
_startTime = Time.time;
}
}
All of this code is found within my FireProjectile class. It is attached to my camera.
Well if I understand you correctly, your game is 2D. Its not important though. I would offer two solutions for your issue:
You can get your pointing direction by using simple vector math. Vector3(to - from). So your arrow pointing direction is : Target.transform.position - Player.transform.position. Now when it is pointing the right direction you can move it like:
Arrow.transform.Translate(Vector3.forward * Time.deltaTime);
Also if your arrow must point to target, but stay in different position than player, just raycast from Player to Target, get raycast point and theres your arrow "to" Vector3.
If arrow always mimic user rotation, you can simply apply rotation to arrow Arrow.transform.rotation = Player.transform.rotation. If rotations don't match (i.e. your object is rotated), just add empty gameobject to mimic rotations and put arrow as child of it. now you can rotate it as you will.
Im working with Xna 4, doing a game where i have a game object (spaceship) that moves in a 3D world on the Y axis = 0 plane.. Aka 2.5D..
Until now i used a very complex angle calculation to create a rotation between 2 vectors, yet that algorithm lacks the ability to take into account that the object already is rotated. so the results get funkey..
Therefore i was hoping that someone, could show me a smart and easily implementable way to use Matrices and vector math, to do such a rotation over time thingy.
What i noticed in previous searches, is that people have the following variables in their object classes:
- Position vector3
- right vector3
- up vector3
- Rotation matrix
- transformMatrix matrix
- velocity vector3
- etc..
often i ask myself why its needed to have that many variables for a simple current position.. or maybe im not understanding.. anyways..
I have the position, rotation and transformsMatrix currently, and would like to know what else i need and HOW to calculate it, and then how you would implement JUST the rotation over time.
The method that is called by my right-click movement command trig sends a vector3 position on the Y = 0 plane of where the click happened.
public void MoveCommand(Vector3 pos){ }
ive tested this, and the "pos" given is accurate. Any help will be apreciated ..
You should check the Matrix.CreateRotationX Y or Z acording to the rotation that you want.
X,Y or Z is the axis of the rotation,
If you choose Y you will see a "2D" rotation (yaw) because that is the axis that you are using as depth.
If you choose X or Z axis you will see "3D" rotations (pitch and roll)
The code should look like this:
WorldMatrix = Rotations * Translation
where
Rotations = Matrix.CreateRotationX(angleRadians)
and
Translation = Matrix.CreateTranslation(Position);
The world matrix is the matrix that is affecting your model, the view and projection depends on the camera
Now if you want to know the angle between vectors you should check the dot product or the atan2 function because you are in 2D
Vector3 Position;
float Rotation;
Matrix World
{
get
{
return Matrix.CreateRotationZ(Rotation) * Matrix.CreateTranslation(Position);
}
}
public void RotateInstantly(Vector3 position)
{
Rotation = Math.Atan2(Position.Y - position.Y, Position.x - position.x);
}
public void RotateIncremently(Vector3 position, float maxStep)
{
float targetRotation = Math.Atan2(Position.Y - position.Y, Position.x - position.x);
float diff = targetRotation - Rotation;
if (Math.Abs(diff) > maxStep)
{
if (targetRotation > Rotation)
Rotation += maxStep;
else
Rotation -= maxStep;
}
else
Rotation = targetRotation;
}
You can use the RotateIncremently like this:*
float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
float maxRotationVelocity = Math.TwoPi; //2* Pi is one revolution.
RotateIncremently(target.Position, maxRotationVelocity * dt);
Thanks to Stig-Rune SkansgÄrd's reply (if your danish hi5), i fixed my old Angle calculation and got it to work in every case.. So i thought i would answer my own question with the solution that works for me, so that future visitors can benefit of it.. This is a snippet of a very large Ship class + a helper method to calculate the angle:
public static float CalculateAngleFromVectors(Vector3 v1, Vector3 v2) {
float x = 0;
float X = v2.X - v1.X,
Z = v2.Z - v1.Z;
if (Z == 0) {
x = (X < 0 ? 90 : 270);
} else if (X == 0) {
x = (Z < 0 ? 180 : -180);
} else {
float temp = MathHelper.ToDegrees((float)Math.Atan(X / Z));
if (X < 0) {
x = (Z < 0 ? Math.Abs(temp) : 180 - Math.Abs(temp));
} else {
x = (Z < 0 ? 360 - Math.Abs(temp) : Math.Abs(temp) + 180);
}
}
return x;
}
this method gets you the float angle (in degrees) to rotate your ship (from the standard 0 degrees starting point)..
to use it, simply make some update / animation method in your class that does something like this:
float desiredRotation = 0, currentRotation = 0, totalElapsed = 0, timePerFrame = 0.05f;
if (desiredRotation != 0) {
totalElapsed += elapsed;
if (totalElapsed > timePerFrame) {
if (isRotationComplete()) {
rotX += MathHelper.ToRadians(desiredRotation);
currentRotation = desiredRotation = 0;
} else if (desiredRotation > currentRotation) {
currentRotation += shipTurnSpeed;
} else if (desiredRotation < currentRotation) {
currentRotation -= shipTurnSpeed;
}
totalElapsed -= timePerFrame;
}
}
EDIT: and the completion check:
private bool isRotationComplete() {
bool b = false;
if (desiredRotation > currentRotation && currentRotation + shipTurnSpeed > desiredRotation) {
b = true;
} else if (desiredRotation < currentRotation && currentRotation - shipTurnSpeed < desiredRotation) {
b = true;
}
return b;
}
essentially what this does is to always check wether or not DesiredRotation is bigger than 0.. if it is, then that means the player has given the command to rotate (or the AI).. CurrentRotation in my example is ofc how much has been rotated since a rotation command was last given, and is set to 0 once the rotation is complete.. You should have a Rotations matrix that uses a different variable to display the rotation with.. mine is this:
public float rotX { get; set; }
public float rotY { get; set; }
public Vector3 position { get; set; }
public Matrix Transform {
get {
return (Matrix.Identity *
Matrix.CreateScale(scale) *
Matrix.CreateRotationY(MathHelper.Pi) *
Rotation *
Matrix.CreateTranslation(position));
}
}
public float ShipCurRotation { get { return (rotX + MathHelper.ToRadians(currentRotation)); } }
public Matrix Rotation { get { return (Matrix.CreateRotationY(ShipCurRotation) * Matrix.CreateRotationX(rotY)); } }
The rotX variable is set in my animation when the rotation is complete, and also at Init.. And here is how i use the rotation angle that my first code snippet generates for me:
public void MoveToPosition(Vector3 pos) {
desiredRotation = (CalculateAngleFromVectors(position, pos) - MathHelper.ToDegrees(rotX));
isMoving = true;
}
.. this makes a smooth customizable rotation, transforms and movement setup.. In a XZ plane ofc.. the Y axis is UP and always 0..
Feel free to comment on this, if you have suggestions or changes or ideas to make things even better.. Im always open for improvements.. Thanks for the replies, and hope this helps alot of new developers, took me forever to gather this stuff from the web..
PS. the rotation can be applied directly to rotX for an instant rotation, bypassing the animation and turnspeed..
WithRegards
MatriXz