Direction entity movment glitch in unity 2d - c#

I am creating a game in Unity 2D. I have Dragons that I am adding to the scene. The dragons are only supposed to move in 1 of 4 directions, Up, Down, Left and Right. the dragons that move left and right move exactly as intended. However the dragons that move up and down have a problem in that they move at an angle. All dragons that move upwards move up and to the right at a 45 degree angle. All dragons that move downwards move down and to the left at a 45 degree angle.
at first I thought it was a problem with the animator moving the dragon to a different location, but I removed the animator component from the prefab and the problem still persisted.
below is the code I am using to move the dragons.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonMovment : MonoBehaviour {
public string Direction; //needs to be set in the prefab
public float DragonSpeed; //speed of dragon
Rigidbody2D rb;
public Transform Boundries;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
float MoveRight = 1;
float MoveLeft = -1;
float MoveUp = 1;
float MoveDown = -1;
if (Direction== "R")
{
rb.velocity = new Vector3(DragonSpeed * MoveRight, rb.velocity.y);
}
if (Direction == "L")
{
rb.velocity = new Vector3(DragonSpeed * MoveLeft, rb.velocity.y);
}
if (Direction == "U")
{
rb.velocity = new Vector3(DragonSpeed * MoveUp, rb.velocity.x);
}
if (Direction == "D")
{
rb.velocity = new Vector3(DragonSpeed * MoveDown, rb.velocity.x);
}
}
}
Edit.
So why does the following work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControler : MonoBehaviour {
// speed of movment
public float Speed;
// rb
Rigidbody2D rb;
public Transform Boundries;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
// get horizontal input
float MoveSide = Input.GetAxis("Horizontal");
//get vertical input
float MoveVert = Input.GetAxis("Vertical");
// move horizontal
rb.velocity = new Vector3(Speed * MoveVert, rb.velocity.y);
// move vertical
rb.velocity = new Vector3(Speed * MoveSide, rb.velocity.x);
}
}
but the other code doesent?

You have got the velocity x in the y of the vector 3
if (Direction == "U")
{
rb.velocity = new Vector3(rb.velocity.x, DragonSpeed * MoveUp);
}
if (Direction == "D")
{
rb.velocity = new Vector3(rb.velocity.x, DragonSpeed * MoveDown);
}
It works in your player script as you are overriding the values in the subsequent statement.
float MoveSide = Input.GetAxis("Horizontal"); //eg 1
float MoveVert = Input.GetAxis("Vertical"); // eg 1
// setting your x velocity incorrectly to the y (vert) velocity speed and keeping y the same velocity as start of frame
rb.velocity = new Vector3(Speed * MoveVert, rb.velocity.y);
// Set the y to the x value of the statement above so it is now in the correct vector and set the x to the correct hoz velocity
rb.velocity = new Vector3(Speed * MoveSide, rb.velocity.x);
// effectively doing
rb.velocity = new Vector3(Speed * MoveSide, Speed * MoveVert);
You should also be using MovePosition as it doesn't directly affect the physics engine (using velocity can have knock on effects to collisions and triggers and create unexpected physics). Your gameobjects will have to be marked as kinematic otherwise the below will cause them to teleport to the new position instantly.
var movementDirection = new Vector3(Speed * MoveSide, Speed * MoveVert);
rb.MovePosition(transform.position + movementDirection * Time.deltaTime);
And the * Time.deltaTime ensures that movement is consistent for different framerates. If you run the game on a 30 fps machine the game objects will move slower than a 60fps. Time.deltaTime calculates the physical time passed since the previous frame and ensures the distance traveled is the same regardless of frame rate.
e.g say the gameObject moves 1 per frame update. After a second on a 30 fps machine the object would have moved 30. After a second on a 60 fps machine the object would have moved 60.
Time.deltaTime=.2s on 30 fps so 1 movement * .2 = move .2 per frame * 30 frames in the second = 60 moved
Time.deltaTime=.1s on 60 fps so 1 movement * .1 = move .1 per frame * 60 frames in the second = 60 moved

Related

Why is my character's horizontal velocity lost when jumping?

GOAL
I'm relatively new to Unity and I want my character to be able to run and jump at the same time causing the character to go up diagonally.
PROBLEM
However after making some adjustments to give the character some acceleration, the jump seems to clear all existing velocity, meaning the character goes up and then to the side instead of both at the same time:
(I apologise if its a bit hard to see)
CODE
This is my character movement script:
Rigidbody2D rb;
BoxCollider2D bc;
[Header("Run")]
float xInput = 0f;
public float maxRunSpeed;
public float acceleration;
[Space]
[Header("Jump")]
public float jumpHeight;
public float lowJumpHeight;
public float fallSpeed;
public float airControl;
[Space]
public LayerMask groundLayer;
public bool onGround;
[Space]
public Vector2 bottomOffset;
public Vector2 boxSize;
public float coyoteTime;
void Start() {
// Gets a reference to the components attatched to the player
rb = GetComponent<Rigidbody2D>();
bc = GetComponent<BoxCollider2D>();
}
void Update() {
Jump();
// Takes input for running and returns a value from 1 (right) to -1 (left)
xInput = Math.Sign(Input.GetAxisRaw("Horizontal"));
}
// Applies a velocity scaled by runSpeed to the player depending on the direction of the input
// Increaces the velocity by accerleration until the max velocity is reached
void FixedUpdate() {
rb.velocity = Math.Abs(rb.velocity.x) < Math.Abs(xInput) * maxRunSpeed ? rb.velocity + new Vector2(acceleration * xInput, rb.velocity.y) * Time.deltaTime : new Vector2(xInput * maxRunSpeed, rb.velocity.y);
}
void Jump() {
// Checks whether the player is on the ground and if it is, replenishes coyote time, but if not, it starts to tick it down
coyoteTime = onGround ? 0.1f : coyoteTime - Time.deltaTime;
// Draws a box to check whether the player is touching objects on the ground layer
onGround = Physics2D.OverlapBox((Vector2)transform.position + bottomOffset, boxSize, 0f, groundLayer);
// Adds an upwards velocity to player when there is still valid coyote time and the jump button is pressed
if (Input.GetButtonDown("Jump") && coyoteTime > 0) {
rb.velocity = Vector2.up * jumpHeight;
}
// Increases gravity of player when falling down or when the jump button is let go mid-jump
if (rb.velocity.y < 0 ) {
rb.velocity += Vector2.up * Physics2D.gravity.y * (fallSpeed - 1) * Time.deltaTime;
} else if (rb.velocity.y > 0 && !Input.GetButton("Jump")) {
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpHeight - 1) * Time.deltaTime;
}
}
Sorry for there being a lot of unecessary code, it's just im not sure what's causing the issue so i don't want to remove anything. Hopefully my comments make some sense?
This is happening because you're setting the velocity of your rigidbody directly with rb.velocity = Vector2.up * jumpHeight. So that will wipe all existing velocity.
If you want to just add a force to the velocity rather than replacing it entirely, you can do that with methods like Rigidbody2D.AddForce.
While in all other cases you keep your velocity values and only slightly modify them you are hard overwriting the absolute velocity in
rb.velocity = Vector2.up * jumpHeight;
erasing any velocity on X.
You could simply keep whatever you have on the other axis and only overwrite Y like
var velocity = rb.velocity;
velocity.y = jumpHeight;
rb.velocity = velocity;
or
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
basically the same way you do it also in FixedUpdate for the horizontal direction.

Making a 2d player look at the cursor, and then move towards it

This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Tweekable move speed
public float MoveSpeed = 4;
// Set what distance it should be from the target
int MaxDist = 10;
int MinDist = 5;
// This is the target
public Transform Mouse;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Keep looking at an object
transform.LookAt();
// Turn off gravity
Physics2D.gravity = Vector2.zero;
// When distance is over minimun
if (Vector3.Distance(Mouse.position, Mouse.position) >= MinDist)
{
// Keep distance
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
// When it's between max and minimun
if (Vector3.Distance(transform.position, Mouse.position) <= MaxDist)
{
}
}
}
// I imported this code from another project so the enemy keeps looking at the player, and keeps his distance. it was a 3D project, so there may be more errors, and this is also my first time creating a game. Be gentle
I am working on a similar project right now.
Here is the code I use to rotate the player (first part) and then move it into that direction (second part):
public Rigidbody2D rb;
public float speed;
void Update()
{
//get mouse position
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//calculating the angle
float AngleRad = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x -
transform.position.x);
float AngleDeg = (180 / Mathf.PI) * AngleRad;
//rotating the player
Player.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
//second part
Vector3 mouseDir = mousePos - transform.position;
mouseDir.z = 0f;
mouseDir = mouseDir.normalized;
rb.AddForce(mouseDir * speed);
}
If the player rotates, but it is looking in the wrong way (in my case), then play around with the the last line of the first part. The following code worked fine for me:
transform.rotation = Quaternion.Euler(0, 0, AngleDeg - 90);
If you want the player to move at a constant speed, then you can use
rb.velocity = mouseDir * speed;
instead of
rb.AddForce(mouseDir * speed);
in the last line of the second part.
Your Player-Gameobject needs a Rigidbody2D component. I also added an if-statement around the second part, so the player only moves to the wanted direction when (for example) space is pressed, but always looks to the mouse.

Unity mid air movement with velocitychange

I have made a script for player movement in a 3D game in Unity.
I like how the movement works, but when I jump while moving, the player keeps moving in that direction at the same speed till it falls to the ground, which is the expected behavior for this script.
But I want to be able to move the player in the air slightly (not fully controllable.)
I'm imagining kinda like a minecraft like movement to be exact.
Any advice to improve this code would be greatly apprecciated.
This is my code.
using System.IO;
using UnityEngine;
public class VelocityMovement : MonoBehaviour
{
#region Variables
public float speed;
public float gravity;
public float maxVelocityChange;
public float jumpHeight;
public float raycastDistance;
public Rigidbody rb;
#endregion
private void Awake()
{
//gets rigidbody, freezes rotation of the rigidbody, disables unity's built in gravity system on rigidbody.
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
rb.useGravity = false;
}
private void Update()
{
//Jump system
if(Grounded())
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector3(rb.velocity.x, CalculateJumpSpeed(), rb.velocity.z);
}
}
}
private void FixedUpdate()
{
//Moves the player when on the ground.
if (Grounded())
{
//Calculate how fast the player should be moving.
Vector3 targetVelocity = new Vector3(playerInputs.hAxis, 0, playerInputs.vAxis);
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed ;
//Calculate what the velocity change should be
Vector3 velocity = rb.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0f;
//applies a force equal to the needed velocity change
rb.AddForce(velocityChange, ForceMode.VelocityChange);
}
//Adds gravity to the rigidbody
rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));
}
//Checks if player is on the ground
private bool Grounded()
{
return Physics.Raycast(transform.position, Vector3.down, raycastDistance);
}
//calculates how high the player should be jumping
private float CalculateJumpSpeed()
{
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
}
private void FixedUpdate()
{
//check how big the movement is. Swap SomeSmallValue with a float like 0.1f
float actualSpeed;
Vector3 velocity;
if(Grounded())
{
actualSpeed = speed;
//HERE IT IS NOT, THIS DOESN'T MATTER
velocity = rb.velocity
}
else{
actualSpeed = speed * SomeSmallValue;
//WITH THIS rb KEEPS THE SPEED IF ANY BUTTON IS PRESSED
velocity = 0;
}
//Moves the player ALWAYS.
//Calculate how fast the player should be moving.
Vector3 targetVelocity = new Vector3(playerInputs.hAxis, 0, playerInputs.vAxis);
targetVelocity = transform.TransformDirection(targetVelocity);
//if Grounded == true the movement is exactly the same than before, because actualSpeed = speed.
//If Grounded == false, the movement is so light
targetVelocity *= actualSpeed;
//Calculate what the velocity change should be
//I'VE PLACED THIS UP IN ORDER TO CALL Grounded() ONLY ONE TIME
//Vector3 velocity = rb.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0f;
//applies a force equal to the needed velocity change
rb.AddForce(velocityChange, ForceMode.VelocityChange);
//Adds gravity to the rigidbody
rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));
}
If your code is working fine, try something like this. Just moved the grounded condition. Now the movement works also at air time. We check with that grounded bool how big the movement is.
If I understood you correctly you want to slow down what your movements do while mid air.
at least that is the case for left and right.
If I did undersand you correctly, you would make an else{} after the if(Grounded())
and go from there, may make some float variable that acts as a multiplier for you left/right control while mid air, aswell as another float variable for forward and backwards.
Inside that else you could just multiply you speed with the variables (depending on the axis) and you should get some form of slower movement there.
I hope that helped you out at least a bit.

Need helping getting a character controller to jump

So far I have a character controller that enables me to move around, sprint and crouch (no animation) , but I am unable to get the controller to jump. I know 100% the character is getting the input to jump, and the movement vector is around ~40 on the Y axis, so the player should be moving. The problem is, nothing happens. The player can still move around, and fall of ledges, but nothing happens when i press space
This is my code:
using UnityEngine;
public class KeyboardMovement : MonoBehaviour
{
private CharacterController controller;
public float walkSpeed;
public float sprintSpeed;
public float crouchSpeed;
public float jumpHeight;
Vector3 up = Vector3.zero;
Vector3 movement = Vector3.zero;
Vector3 forward = Vector3.zero;
Vector3 sideways = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float speed = walkSpeed;
//If crouching, set speed to crouch speed. This ovverides sprinting
if (SingletonSettings.GetKey(SingletonSettings.Keybindings.crouch))
speed = crouchSpeed;
//Else if sprinting, set speed to sprint speed
else if (SingletonSettings.GetKey(SingletonSettings.Keybindings.sprint))
speed = sprintSpeed;
//Create vectors for movement
forward = transform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical");
sideways = transform.TransformDirection(Vector3.right) * Input.GetAxis("Horizontal");
//up = SingletonSettings.GetKey(SingletonSettings.Keybindings.jump) && controller.isGrounded ? Vector3.up * jumpHeight : Vector3.zero;
movement = (up * 100) + ((forward + sideways) * 10 * Time.deltaTime * speed);
//Combine vectors and Multiply by DeltaTime to ensure smooth movement at different framerates.
//Also multiply by 10*speed to ensure correct speed in different states
if (controller.isGrounded && Input.GetKey(KeyCode.Space))
{
movement.y = jumpHeight*50 * Time.deltaTime;
}
controller.SimpleMove(movement);
}
void OnGUI()
{
GUILayout.Label('\n' + Newtonsoft.Json.JsonConvert.SerializeObject(movement.y, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
}));
}
}
CharacterController.SimpleMove from the docs
Moves the character with speed.
Velocity along the y-axis is ignored.
Gravity is automatically applied.
https://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html
You should use CharacterController.Move
A more complex move function taking absolute movement deltas.
Attempts to move the controller by motion, the motion will only be constrained by collisions. It will slide along colliders
...
This function does not apply any gravity.
https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
With this method, you'll need to apply gravity yourself
movement.y -= gravity * Time.deltaTime;
controller.Move(movement * Time.deltaTime);

Enemy object moves through objects

I have been trying to find a solution to my problem involving a game object in my scene that walks through elevated terrain and rocks. The rocks have colliders and the enemy object has a collider and a rigid body. I have attached my script which moves the enemy around when the FPS gets withing a certain range. This all works fine but its walking through rocks and elevated terrain. I have tried many suggestions so far but nothing seems to work.
using UnityEngine;
using System.Collections;
public class CreatureWalk : MonoBehaviour {
public int totalSecs = 0;
public GameObject explosion;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
GameObject GM = GameObject.Find ("GameManager");
//Get Game Manager Component on Object
GameManager GM_Component = GM.GetComponent<GameManager> ();
if (GM_Component.GameOver) {
animation.Play ("idle");
} else if (GM_Component.FlowerHits > 30) {
while (totalSecs < 5000) {
animation.Play ("hit");
totalSecs += 1;
}
//Destroy (gameObject);
//create explosion
Instantiate (explosion, transform.position, transform.rotation);
Invoke("Disable",3);
Invoke("Reenable",3);
Vector3 currentPosition = this.transform.position; //get current position of creature
transform.position = new Vector3(currentPosition.x+ Random.Range(-20.0F, 20.0F),0.2f,currentPosition.z +Random.Range(-20.0F, 20.0F));
totalSecs =0;
GM_Component.FlowerHits =0;
} else {
animation.Play ("walk");
MoveTowardsTarget ();
}
}
private void Disable() {
gameObject.SetActive(false);
//Debug.Log ("Disable");
}
private void Reenable() {
gameObject.SetActive(true);
//Debug.Log ("Re-enable");
}
//move towards a target at a set speed.
private void MoveTowardsTarget() {
//the speed, in units per second, we want to move towards the target
float speed = 1;
//move towards the fps
Vector3 targetPosition = new Vector3(Camera.main.transform.position.x,0,Camera.main.transform.position.z);
Vector3 currentPosition = this.transform.position;
transform.LookAt (targetPosition);
//first, check to see if we're close enough to the target
if (Vector3.Distance (currentPosition, targetPosition) < 30f) {
Vector3 directionOfTravel = targetPosition - currentPosition;
directionOfTravel.Normalize ();
this.transform.Translate (
(directionOfTravel.x * speed * Time.deltaTime),
(directionOfTravel.y * speed * Time.deltaTime),
(directionOfTravel.z * speed * Time.deltaTime),
Space.World);
//this.rigidbody.MovePosition(directionOfTravel);
// (directionOfTravel.x * speed * Time.deltaTime),
// (directionOfTravel.y * speed * Time.deltaTime),
// (directionOfTravel.z * speed * Time.deltaTime));
//this.rigidbody.AddForce(
// (directionOfTravel.x * speed * Time.deltaTime),
// (directionOfTravel.y * speed * Time.deltaTime),
// (directionOfTravel.z * speed * Time.deltaTime),ForceMode.VelocityChange);
//rigidbody.AddForce(transform.forward * 10);
} else
animation.Play ("idle");
}
}
I have tried to move with AddForce as suggested as you can see by the commented code but the enemy just walks in place.
The creature has a rigidbody, gravity and kinematics checked.
Try adding 1000 or 10000 force ... from the looks of it you're using fractions of 1... if you have a mass of 1 it still takes over 1 force to make it move. If you add 1000 force and his mass is 1 and he still doesn't move, then make sure he's not set as kinematic in his rigidbody.
If you still aren't moving, something much more insidious is going on. I'd suggest checking you're using the right axis, the right direction.. etc.. things that just couldn't possibly be wrong.
Good luck, hope that helped.
Whenever you're using AddForce, try to multiply the directional Vector with your desired speed and the object's Rigidbody mass. Also, uncheck the "isKinematic" checkbox because that kind of "deactivates" the physics engine's calculations on that object. If you want to be accurate you can also manually set the velocity of the object's Rigidbody too.

Categories

Resources