How to get 8 direction Movement with controller sticks | Topdown 3D Unity - c#

Im working on an 3d rpg top down view in Unity. Something like the zelda links awakening remake.
What im trying to achieve ist that the player rotates to the direction you press and then just goes forwards and this in only 8 directions.
I already got this working with WASD and the dpad, there it obviously works because you cant press in between two buttons if you know what i mean.
But i need a way to clamp the joystick input to only the 8 directions. How can i achieve this ? I hope you understand what i mean. This is the code ive already written. Note that im using the new input system.
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private PlayerInputActions playerInput;
private Rigidbody rb;
[SerializeField]
private float playerSpeed;
private float angle;
private Quaternion targetRotation;
private Vector2 input;
private Transform cam;
void Awake()
{
playerInput = new PlayerInputActions();
rb = GetComponent<Rigidbody>();
}
void Start()
{
cam = Camera.main.transform;
}
void Update()
{
GetInput();
if (input.x == 0 && input.y == 0) return;
CalculateDirection();
Rotate();
Move();
}
void GetInput()
{
input.x = playerInput.Player.Move.ReadValue<Vector2>().x;
input.y = playerInput.Player.Move.ReadValue<Vector2>().y;
}
void CalculateDirection()
{
if (input.sqrMagnitude > 1.0f)
input = input.normalized;
angle = Mathf.Atan2(input.x, input.y);
angle = Mathf.Rad2Deg * angle;
angle += cam.eulerAngles.y;
}
void Rotate()
{
targetRotation = Quaternion.Euler(0, angle, 0);
transform.rotation = targetRotation;
}
void Move()
{
//transform.position += transform.forward * 5 * Time.deltaTime;
rb.velocity = transform.forward * 200 * Time.fixedDeltaTime;
}
void OnEnable()
{
playerInput.Enable();
}
void OnDisable()
{
playerInput.Disable();
}
} ```

You can do something like this to keep results in the same way that you're working at moment
private void CalculateDirection (InputAction.CallbackContext context)
{
var input = context.ReadValue<Vector2>();
var x = Mathf.Abs(input.x) <= .1f ? 0f : // Deadzone
input.x > 0 ? 1f : -1f; // Direction
var y = Mathf.Abs(input.y) <= .1f ? 0f :
input.y > 0 ? 1f : -1f;
var angle = Mathf.Atan2(x, y);
angle = Mathf.Rad2Deg * angle;
}
As you are working with gamepad stick and values here can be a range between -1..1 and when you get interactions from your keyboard buttons values are coming in 0, 1, or -1, similar to the Input.GetAxisRaw() in older input system, we don't need to normalize it.
This approach should work but I have a feeling that this will give some weird behaviors when controlling with the stick, I mean, maybe if you try to go up the player ends going to some side (UpRight or UpLeft) because the range for upper, down, right and left aren't really wide, I hope to be wrong but if I'm right you'll probably need to bite the bullet and manually decide a range/angle for each of the 8 sides. Try the above code and if you need help manually creating it just come back here and we try again.
N

Related

Swerve Control Unity C#

Hi I am creating a hyper casual game with unity, but I have encountered a problem with the swerve control (I have also seen many git hubs but even these do not work perfectly)
I've put this in my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float lastframeposx;
private float movefactorx;
public float MoveFactorX => movefactorx;
public Camera m_MainCam;
private float speed = 2.0f;
[SerializeField]
GameObject character;
[SerializeField] private float swerveSpeed = 0.5f;
[SerializeField] private float maxSwerveAmount = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position += Vector3.forward * speed * Time.deltaTime;
Cammina();
/*Vector3 destra = Camera.main.ScreenToWorldPointt(Input.touches[i].position);
transform.position += Vector3.zero destra;*/
}
void Cammina()
{
if(Input.GetMouseButtonDown(0))
{
lastframeposx = Input.mousePosition.x;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
else if (Input.GetMouseButton(0))
{
movefactorx = Input.mousePosition.x - lastframeposx;
lastframeposx = Input.mousePosition.x;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
else if(Input.GetMouseButtonUp(0))
{
movefactorx = 0f;
float swerveAmount = Time.deltaTime * swerveSpeed * MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
}
}
/*void vaidoveschiacciato()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.collider != null)
{
transform.position += hit.collider.GetComponent<Transform.position> * speed * Time.deltaTime;
}
}
}
}*/
}
1 Problem: he don't go when the finger is
2 Problem: How do I eliminate the movement from right to left (Without making it go out of the path)
(Langauge: C#)
The problem: When you swerve, it swerves just in the direction, there is no limits on how far it goes.
How I would fix this: I would put the movement to change it through a function. This could clamp it, so the higher the distance to the center of the track, the less it swerves. Or, you can altogether check if the distance is a maximum and then stop swerving.
Note: you can use other functions to do this (they just have to flatten out the larger the input).
Smooth, good looking bell curve way
For example you could use a bell curve. Look one up if you've never seen one before. It is at it's highest possible output of one, at a zero input. When it gets hiher or lower, it outputs lower to zero.
the simplest equation is y = i-(x2). The lower i is (above 1), the wider the curve, or the larger the output is for a larger input. I made a graph here.
x can be the distance to the center of the track, so you should adjust i, so the maximum distance from the track is flat.
This output is what you should change swerveAmount to.
The flatter parts of the graoh is how much you will swerve when you are that distance from the center (x axis)
Alternatively
You could just check the distance, and if it passes a certain distance don't translate it.
Let me know in the omments if there are any problems! :)

Need support with Unity Enging C# Scripts for Player Movement

I've been recently working on a project using Unity Engine that involves a sort of Top-Down, 3rd person view, and I have been having trouble with the character movement.
I want to implement a way for the player to move throughout the map using either WASD movement, Click-to-Move movement, or Drag-to-Move movement, allowing them to use either of these freely and at any time, yet I have not yet found a way of doing this, since the methods end up cancelling each other out, leading the Player Character to awkward movement and to getting stuck in place.
Is there any way of achieving this? If so, any tips/suggestions would be greatly appreciated. Please keep in mind I am a complete beginner when it comes to both Unity and C#, so I might not grasp some core concepts yet.
I have attached my PlayerMovement C# code below.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(test))]
public class PlayerMovement : MonoBehaviour
{
private test _input;
//click2move
private NavMeshAgent agent;
//x
[SerializeField]
private bool RotateTowardMouse;
[SerializeField]
private float MovementSpeed;
[SerializeField]
private float RotationSpeed;
[SerializeField]
private Camera Camera;
void Start()
{
//c2m
agent = GetComponent<NavMeshAgent>();
//x
}
private void Awake()
{
_input = GetComponent<test>();
}
// Update is called once per frame
void Update()
{
var targetVector = new Vector3(_input.InputVector.x, 0, _input.InputVector.y);
var movementVector = MoveTowardTarget(targetVector);
agent.autoBraking = true;
if (!RotateTowardMouse)
{
RotateTowardMovementVector(movementVector);
}
if (RotateTowardMouse)
{
RotateFromMouseVector();
}
//c2m
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0))
{
agent.SetDestination(hit.point);
}
}
else
{
if (agent.remainingDistance < 1)
{
agent.ResetPath();
}
}
}
private void RotateFromMouseVector()
{
Ray ray = Camera.ScreenPointToRay(_input.MousePosition);
if (Physics.Raycast(ray, out RaycastHit hitInfo, maxDistance: 300f))
{
var target = hitInfo.point;
target.y = transform.position.y;
transform.LookAt(target);
}
}
private Vector3 MoveTowardTarget(Vector3 targetVector)
{
var speed = MovementSpeed * Time.deltaTime;
// transform.Translate(targetVector * (MovementSpeed * Time.deltaTime)); Demonstrate why this doesn't work
//transform.Translate(targetVector * (MovementSpeed * Time.deltaTime), Camera.gameObject.transform);
targetVector = Quaternion.Euler(0, Camera.gameObject.transform.rotation.eulerAngles.y, 0) * targetVector;
var targetPosition = transform.position + targetVector * speed;
transform.position = targetPosition;
return targetVector;
}
private void RotateTowardMovementVector(Vector3 movementDirection)
{
if (movementDirection.magnitude == 0) { return; }
var rotation = Quaternion.LookRotation(movementDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, RotationSpeed);
}
}
I would highly recommend that you use the Unity Input System. It can automate switching between input devices and decouple your code from any specific controls. It might be a bit overwhelming if you have limited experience with C#, but there is a vast library of tutorial videos and written guides you can rely on.
The PlayerInput component is what detects when different input devices are added or removed and switches to the first valid control scheme. When a user activates the inputs required to trigger something in your game, that is represented as an Action. A set of callbacks for that action are called depending on the states of the input: started, performed, and canceled. These are where you hook up your character controller to the input and run any extra logic necessary to turn an input into movement. The Input System also has a feature called interactions that will probably be useful, especially for drag-to-move controls.
Good luck! If you'd like me to explain something further or point you to a good tutorial, feel free to tag me in a comment.
First create a Capsule in the scene, drag the main camera to its object, hang the script under the Capsule object, WASD controls the movement direction, the space moves up along the Y axis, and the F moves down along the Y axis, thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCam : MonoBehaviour
{
private Vector3 m_camRot;
private Transform m_camTransform;//Camera Transform
private Transform m_transform;//Camera parent object Transform
public float m_movSpeed=10;//Movement factor
public float m_rotateSpeed=1;//Rotation factor
private void Start()
{
m_camTransform = Camera.main.transform;
m_transform = GetComponent<Transform>();
}
private void Update()
{
Control();
}
void Control()
{
if (Input.GetMouseButton(0))
{
//Get the mouse movement distance
float rh = Input.GetAxis("Mouse X");
float rv = Input.GetAxis("Mouse Y");
// rotate the camera
m_camRot.x -= rv * m_rotateSpeed;
m_camRot.y += rh*m_rotateSpeed;
}
m_camTransform.eulerAngles = m_camRot;
// Make the main character face in the same direction as the camera
Vector3 camrot = m_camTransform.eulerAngles;
camrot.x = 0; camrot.z = 0;
m_transform.eulerAngles = camrot;
// Define 3 values to control movement
float xm = 0, ym = 0, zm = 0;
//Press W on the keyboard to move up
if (Input.GetKey(KeyCode.W))
{
zm += m_movSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.S))//Press keyboard S to move down
{
zm -= m_movSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))//Press keyboard A to move left
{
xm -= m_movSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D))//Press keyboard D to move right
{
xm += m_movSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Space) && m_transform.position.y <= 3)
{
ym+=m_movSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.F) && m_transform.position.y >= 1)
{
ym -= m_movSpeed * Time.deltaTime;
}
m_transform.Translate(new Vector3(xm,ym,zm),Space.Self);
}
}

How can I refactor my 2d unity movement script to resolve the behavioral issues I have?

Below is the code I've written for my 2D movement:
void HandleInput()
{
float verticalMoveAmount = Input.GetAxis("Vertical") * playerSpeed * Time.deltaTime;
float horizontalMoveAmount = Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime;
if (facingRight)
{
transform.Translate(horizontalMoveAmount, verticalMoveAmount, 0);
}
else
{
transform.Translate(-horizontalMoveAmount, verticalMoveAmount, 0);
}
if (horizontalMoveAmount > 0 && !facingRight)
{
Flip();
}
else if (horizontalMoveAmount < 0 && facingRight)
{
Flip();
}
if (Input.GetKeyDown(KeyCode.Space) && hasBall)
{
Shoot();
}
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
I have the below problems though:
I have a Cinemachine virtual camera following the player around. However, whenever the player changes direction they vanish from the camera view. I think this is something to do with the Flip(), but I don't know why or how to fix it.
The if statement for if facingRight where I have to use negative movement on the horizontal axis, this feels like a really poor way to handle it, but if I don't do that I can't move left when the player changes directions to the left. Is there a better way I can do this?
Thank you in advance.
To 1) my guess is that since you rotate the object by 180° you are looking at the backface of it -> it is not being rendered due to backface culling.
To 2) since you rotate the object by 180° it's local coordinate system is now pointing in the other direction. Note that by default Transform.Translate works in the local coordinate system. If you want to rather use global axes then simply pass in
transform.Translate(horizontalMoveAmount, verticalMoveAmount, 0, Space.World);
In general simply assuming you are using a SpriteRenderer which is the most common thing for 2D games you should rather use the property SpriteRenderer.flipX and not rotate your object at all. This would solve both issues at the same time
So together your code could be simply
public SpriteRenderer spriteRenderer;
void HandleInput()
{
var input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
// ensure that diagonal movement isn't faster
input = Vector2.ClampMagnitude(input, 1f);
var movement = input * playerSpeed * Time.deltaTime;
transform.Translate(movement, Space.World);
// basically the same now as
//transform.position += movement;
if (input.x != 0)
{
spriteRenderer.flipX = input.x < 0;
}
if (hasBall && Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}

When fliping the character the rotation of character's weapon goes backward

I have two game objects on the Scene, 1.Character(Parent Object) and 2.Weapon(Child Object) . The problem is when the character is moving to the right side, the rotation of the weapon is fine, it is toward where character is facing and rotating as expected, as you can see in the Gif image attach below. But when i Flip to left side everything goes wrong, the weapon goes backward and when i press down arrow the rotation goes up and when press up arrow the rotation goes down, see the Gif image attach below.. Please help How to Fix it.
Here is my Code:
public float weaponRotationSpeed = 13f;
private Animator anim;
private float angle;
void Awake()
{
anim = GetComponent<Animator>();
}
void Update()
{
Vector2 hv = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Vector3 changeParentScale = transform.localScale;
if (hv != Vector2.zero)
{
if (Input.GetAxis("Horizontal") < 0)
{
changeParentScale.x = -5f;
transform.localScale = changeParentScale;
}
else if (Input.GetAxis("Horizontal") > 0)
{
changeParentScale.x = 5f;
transform.localScale = changeParentScale;
}
angle = Mathf.Atan2(hv.y, hv.x) * Mathf.Rad2Deg;
transform.Find("Weapon").rotation = Quaternion.Lerp(transform.Find("Weapon").rotation,
Quaternion.Euler(0, 0, angle),
weaponRotationSpeed * Time.deltaTime);
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
well you probably would want to flip the rotation then as well? E.g. using Mathf.Sign
Quaternion.Euler(0,0, angle * Mathf.Sign(changeParentScale.x))
There are some other little flaws in your code!
You shouldn't use Find each frame .. rather store it once.
You are using GetAxis repeatedly and should also store the vaues the first time .. don't you already have them in hv?
Lerp is quite cool tool but you are using it wrong ;) It interpolates each frame using the given factor .. usually you want a fixed one like e.g. 0.5f and not using Time.deltaTime
It should probably rather be something like
// You will have to adjust this value again
// This needs to be a constant value between 0 and 1
// - 0: rotation isn't updated at all
// - 1: rotation immediately jumps to target
// - e.g. 0.5f: rotation is every frame set to the middle between current and target
[Range(0f, 1f)]
public float weaponRotationSpeed = 0.5f;
// already reference these via the Inspector
[SerializeField] private Animator anim;
[SerializeField] private Transform weapon;
private float angle;
// As Fallback get them ONCE on runtime
void Awake()
{
if(!anim) anim = GetComponent<Animator>();
if(!weapon) weapon = transform.Find("Weapon");
}
void Update()
{
Vector2 hv = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
Vector3 changeParentScale = transform.localScale;
if (hv != Vector2.zero)
{
// get direction of Horizontal
int sign = Mathf.Sign(hv.x);
if (!Mathf.Approximately(hv.x, 0))
{
changeParentScale.x = 5f * sign;
transform.localScale = changeParentScale;
}
angle = Mathf.Atan2(hv.y, hv.x) * Mathf.Rad2Deg;
weapon.rotation = Quaternion.Lerp(weapon.rotation, Quaternion.Euler(0, 0, angle * sign), weaponRotationSpeed);
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
}
Alternatively you could probably also already solve it by not using rotation but localRotation for rotating the weapon.

Unity jumping fails while going against a wall

Okay im trying to make my object (player) to jump everything is okay until i go against a wall and keep going against (still W is down) i cant jump wen im hitting a wall if i stop walking he will be enable to jump i tried making the walls on touch to make the player to have velocity = zero but it does not work,
i tried to add rigid body to the walls and freezing them in place, trying to make them kinematic does not work too .
I wish wen i go against walls and keep walking against them to be enable to jump.
If you know how i can do that please share thanks .
Here is the move script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveScript : MonoBehaviour {
private float speed;
private float jumpHight;
private float straffeSpeed;
private float fallMultiplier;
private Rigidbody rig;
private Collider coll;
// Use this for initialization
private void Awake()
{
rig = GetComponent<Rigidbody>();
coll = GetComponent<Collider>();
straffeSpeed = 1.5f;
fallMultiplier = 2.5f;
speed = 10f;
jumpHight = 4f;
}
void Start () {
GroundCheck();
}
// Update is called once per frame
void Update () {
Move();
GroundCheck();
BetterFall();
}
private void Move()
{
float hAxis = Input.GetAxis("Horizontal") * straffeSpeed;
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
rig.MovePosition(transform.position + movement);
if (Input.GetKey(KeyCode.Space) && GroundCheck())
{
rig.velocity = Vector3.up * jumpHight;
}
}
private bool GroundCheck()
{
return Physics.Raycast(transform.position, -Vector3.up, coll.bounds.extents.y + 0.2f);
}
private void BetterFall()
{
if(rig.velocity.y < 0)
{
rig.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
}
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
rig.velocity = Vector3.up * jumpHight;
}
I don't think you are doing this quite right. Try this:
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
rig.AddForce(Vector3.up * jumpHight, ForceMode.Impulse);
}
:-)

Categories