Unity 3rd person controller, rotation nightmare - unity3d

I am trying to make a MMO character controller like the one of this Youtube video:https://www.youtube.com/watch?v=fOvf7gRO_aM
Basically, you use WASD to move around.
You can move the camera by mouse click and drag, and when moving, you character will now move in the new camera direction.
The thing is, i would like that when i press WASD, that the character (not the camera) would face the direction of the mouvement.
I tried to use this:
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.localRotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
The character was not facing the right direction, so i tried this
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.LookRotation(movement);
}
but that does not seem to work. I am a noob after all :D
Here is the full code of the controller's move part:
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
movement = character.rotation * movement;
characterController.Move(movement * Time.deltaTime);
//Animation on move
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);
//The place where things go south it seems
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
{
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
I had a previous version on the controller, without the change of camera with mouse, but the right character's behaviour facing the direction of the input:
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
characterController.Move(movement * Time.deltaTime);
if (movement != Vector3.zero) transform.rotation = Quaternion.LookRotation(movement);
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
playerCamera.position = new Vector3(character.position.x, character.position.y + yCamera, character.position.z + zCamera);
}
Also, here is the mousemoving part:
void MouseTurnAround()
{
zoom += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
if (zoom > zoomMin)
zoom = zoomMin;
if (zoom < zoomMax)
zoom = zoomMax;
playerCamera.transform.localPosition = new Vector3(0, 0, zoom);
if (Input.GetMouseButton(0))
{
mouseX += Input.GetAxis("Mouse X");
mouseY -= Input.GetAxis("Mouse Y");
}
mouseY = Mathf.Clamp(mouseY, -60f, 60f);
playerCamera.LookAt(centerPoint);
centerPoint.localRotation = Quaternion.Euler(mouseY, mouseX, 0);
}
I don't really have more ideas, so maybe smart people can see what they can do ! Thanks in advance..

I managed some kind of a mix of my 2 solutions, and parented my character to an empty gameobject, which receives the new rotations. Then, i leave the character be oriented to the movement with look rotation.
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
movement = character.rotation * movement;
characterController.Move(movement * Time.deltaTime);
//Animation on move
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);
//Rotates the character to move towards new direction
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
{
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
if (movement != Vector3.zero) lkModel.rotation = Quaternion.LookRotation(movement);
}

Related

Unity Setting Animator Parameters

I'm trying to animate my player in Unity using the animator component but for some reason it seems that my character is stuck in the idle animation and I can't change the animation parameters from my code. I'm using a 2D freeform direction blend tree in unity, and I'm not exactly sure why it's not working. The animations themselves seem to be working fine when I change the parameters using the inspector.
`
void Update()
{
groundedPlayer = characterController.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector2 input = moveAction.ReadValue<Vector2>();
Vector3 move = new Vector3(input.x, 0, input.y);
move = move.x * cameraTransform.right.normalized + move.z * cameraTransform.forward.normalized;
move.y = 0f;
animator.SetFloat(moveXAnimationParameterId, input.x);
animator.SetFloat(moveZAnimationParameterId, input.y);
characterController.Move(move * Time.deltaTime * playerSpeed);
if (jumpAction.triggered && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
if (move != Vector3.zero)
{
animator.Play(runningJumpAnimation);
}
else
{
animator.Play(jumpAnimation);
}
}
playerVelocity.y += gravityValue * Time.deltaTime;
characterController.Move(playerVelocity * Time.deltaTime);
float targetAngle = cameraTransform.eulerAngles.y;
Quaternion targetRotation = Quaternion.Euler(0, targetAngle, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
instead of using animator.Play I suggest you use conditions using for example :
_anim.SetBool("isRunning",true);

Move Object around a platform of tiles in Unity

I want to make a spinning spike to move around a platform made out of tiles like in the following
I have written every possible state on where to move when platform tiles block the spike's way. It would look something like this
for (int i = 0; i < platformTileMap.Length; i++)
{
if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(0, -1, 0))) // BOTTOM
{
moveX = moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(0, 1, 0))) // TOP
{
moveX = -moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(-1, -1, 0))) //BOT LEFT
{
if (moveDir == 1)
{
moveY = -1;
}
else moveX = moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(1, 1, 0))) //TOP RIGHT
{
if (moveDir == 1)
{
moveY = 1;
}
else moveX = -moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(1, -1, 0))) // BOT RIGHT
{
if (moveDir == 1)
{
moveX = moveDir;
}
else
{
moveY = -1;
}
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(-1, 1, 0))) // TOP LEFT
{
if (moveDir == -1)
{
moveY = 1;
}
else
{
moveX = -moveDir;
}
}
I feel like there has to be a more efficient way to solve this. Do I really have to write every possibility in if statements? Can I achieve this with pathfinding?
How about this
Use raycast
Place Empty GameObject and rotate spike when it arrives them
You could create an array of points to move to.
public Vector3[] movePoints;
You can set movePoints in inspector or in code (such as Start function) user choice
In the update loop lerp to the next point in sequence, when arrived pull the next point, unless we are at the end of the array then pull the first point in the array, rinse and repeat forever.
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
Set this up properly once, when you make more blades and different configurations or want to change speed it will be ez pz.
For anyone interested. Here is how i solved it:
GridLayout platformGridLayout;
Grid platformGrid;
Tilemap[] platformTileMap;
[SerializeField] float rotationSpeed;
[SerializeField] float moveSpeed;
[SerializeField] private LayerMask platformLayerMask;
Vector3Int startPos;
Vector3Int currentCellPosition;
Vector3 raycastPlatformDir;
Vector3 raycastMoveDir;
float platformRaycastDist;
// Start is called before the first frame update
void Start()
{
movePoints = new List<Vector3Int>();
platformGridLayout = transform.parent.GetComponentInParent<GridLayout>();
platformGrid = transform.parent.GetComponentInParent<Grid>();
startPos = platformGridLayout.WorldToCell(transform.position);
Debug.Log("Cell Startposition: " + startPos);
PlatformToMoveOn();
GetStartRaycastDir();
platformRaycastDist = platformGridLayout.cellSize.x;
Debug.Log("CellCenterToWorld of Startposition: " + platformGrid.GetCellCenterWorld(startPos));
Debug.Log(platformGrid.GetCellCenterLocal(currentCellPosition + Vector3Int.FloorToInt(raycastPlatformDir) + Vector3Int.FloorToInt(raycastMoveDir)));
}
private void PlatformToMoveOn()
{
platformTileMap = new Tilemap[2];
platformTileMap[0] = GameObject.Find("Platform").GetComponent<Tilemap>();
platformTileMap[1] = GameObject.Find("MovingPlatform").GetComponent<Tilemap>();
}
private void GetStartRaycastDir()
{
for (int i = 0; i < platformTileMap.Length; i++)
{
if (platformTileMap[i].HasTile(startPos + new Vector3Int(0, -1, 0))) // BOTTOM
{
raycastPlatformDir = Vector3.down;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(0, 1, 0))) // TOP
{
raycastPlatformDir = Vector3.up;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(1, 0, 0))) // RIGHT
{
raycastPlatformDir = Vector3.right;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(-1, 0, 0))) // LEFT
{
raycastPlatformDir = Vector3.left;
}
}
raycastMoveDir = Quaternion.Euler(0, 0, 90) * raycastPlatformDir * Mathf.Sign(moveSpeed);
//raycastMoveDir = new Vector3(raycastPlatformDir.y, raycastPlatformDir.x) * Mathf.Sign(moveSpeed);
}
// Update is called once per frame
void Update()
{
MoveSpike();
}
private void MoveSpike()
{
currentCellPosition = platformGridLayout.WorldToCell(transform.position); // + raycastPlatformDir * platformGridLayout.cellSize.y / 2;
// Debug.Log(cellPosition);
//Debug.Log(raycastMoveDir);
transform.Rotate(0, 0, 300 * rotationSpeed * Time.deltaTime);
RaycastHit2D raycastMove = Physics2D.Raycast(platformGrid.GetCellCenterLocal(currentCellPosition),raycastMoveDir,0.01f,platformLayerMask);
RaycastHit2D raycastPlatform = Physics2D.Raycast(platformGrid.GetCellCenterLocal(currentCellPosition), raycastPlatformDir, platformRaycastDist, platformLayerMask);
Debug.DrawRay(transform.position, raycastMoveDir * 0.01f, Color.red);
Debug.DrawRay(transform.position, raycastPlatformDir * platformRaycastDist, Color.green);
if (currentCellPosition != startPos) { // Check on Platform corners
Debug.Log("Checking");
if (raycastMove.collider != null)
{
// reassign raycastsdirections
RotateRaycastDirections(1);
Debug.Log("Spike Collision");
}
else if (raycastPlatform.collider == null)
{
RotateRaycastDirections(-1);
Debug.Log("Spike on Platform");
}
startPos = currentCellPosition;
}
/*transform.position = Vector3.MoveTowards(transform.position,
platformGrid.GetCellCenterLocal(currentCellPosition + Vector3Int.FloorToInt(raycastPlatformDir) + Vector3Int.FloorToInt(raycastMoveDir)), moveSpeed * Time.deltaTime); */
transform.Translate(raycastMoveDir.x * Mathf.Abs(moveSpeed) * Time.deltaTime, raycastMoveDir.y * Mathf.Abs(moveSpeed) * Time.deltaTime, 0, Space.World);
}
private void RotateRaycastDirections(int angle)
{
raycastPlatformDir = Quaternion.Euler(0, 0, 90) * raycastPlatformDir * angle * Mathf.Sign(moveSpeed);
raycastMoveDir = Quaternion.Euler(0, 0, 90) * raycastMoveDir * angle * Mathf.Sign(moveSpeed);
// raycastPlatformDir = new Vector3(raycastPlatformDir.y, raycastPlatformDir.x) * angle;
//raycastMoveDir = new Vector3(raycastMoveDir.y, raycastMoveDir.x) * -angle;
}
Edit:
This doesnt work on moving Platforms though. Any ideas how i could fix that? I tried changing the raycast position to the tilemapscentercell position but it doesnt work.

Making a volleyball game, overlap not working

I'm making a 2D game on Unity and I created an overlap to see if the ball is on the hit area. However, whenever I push the hit button, the ball is hit even if it is away of the player. I am sending the code for the Player, which contains what is needed so far, to see what may have gone wrong. Supposedly with the foreach hitCollider in hitColliders it should be fixed but it is not working I and just canĀ“t wrap my mind around it.
public int speed;
public bool estoyFloor, subiendo, bajando;
public bool canRight, canLeft;
public bool canMove;
public float posYinit;
public Vector3 posInitPlayer;
bool m_Started;
public LayerMask m_LayerMask;
public GameObject volleyBall;
// Use this for initialization
void Start () {
m_Started = true;
}
void FixedUpdate()
{
Collider2D[] hitColliders = Physics2D.OverlapBoxAll(gameObject.transform.position, transform.localScale / 2, 0);
foreach (Collider2D hitCollider in hitColliders)
{
if (Input.GetButtonDown("Fire1"))
{
volleyBall.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
volleyBall.GetComponent<Rigidbody2D>().AddForce(new Vector3(300, 500));
}
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
if (m_Started)
Gizmos.DrawWireCube(transform.position, transform.localScale / 2);
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
canMove = true;
if (Input.GetAxisRaw("Horizontal") > 0 && canRight)
transform.position += Vector3.right * speed * Time.deltaTime;
if (Input.GetAxisRaw("Horizontal") < 0 && canLeft)
transform.position += Vector3.left * speed * Time.deltaTime;
if (Input.GetButtonDown("Jump") && estoyFloor)
{
subiendo = true;
posYinit = transform.position.y;
}
if (transform.position.y > posYinit + 2.5f)
{
subiendo = false;
bajando = true;
}
if (!estoyFloor && !subiendo)
bajando = true;
Debug.DrawLine(transform.position, transform.position + new Vector3(0, -1, 0), Color.red);
Debug.DrawLine(transform.position, transform.position + new Vector3(0.8f, 0, 0), Color.red);
Debug.DrawLine(transform.position, transform.position + new Vector3(1.5f, 0, 0), Color.red);
RaycastHit2D[] hitDown = Physics2D.LinecastAll(transform.position, transform.position + new Vector3(0, -1, 0));
RaycastHit2D[] hitsRight = Physics2D.LinecastAll(transform.position, transform.position + new Vector3(0.8f, 0, 0));
RaycastHit2D[] hitsLeft = Physics2D.LinecastAll(transform.position, transform.position + new Vector3(1.5f, 0, 0));
estoyFloor = false;
foreach (RaycastHit2D hit in hitDown)
{
if (hit.collider.name == "Floor")
{
bajando = false;
estoyFloor = true;
}
}
canRight = true;
foreach (RaycastHit2D hit in hitsRight)
{
if (hit.collider.tag == "Wall")
canRight = false;
}
canLeft = true;
foreach (RaycastHit2D hit in hitsLeft)
{
if (hit.collider.tag == "Wall")
canLeft = false;
}
if (subiendo)
transform.position += Vector3.up * speed * Time.deltaTime;
if (bajando)
transform.position += Vector3.down * speed * Time.deltaTime;
}
Your overlap box is probably colliding with the player it is on. Because you do not check if the collider belonged to the volleyball in your loop, you are hitting the ball no matter what. Try to get the transform from the collider and check its tag to see if it is a volleyball before adding force to the ball and see if that works. You could also use a layermask on your Overlap box check as well to ensure only volleyballs get detected.

Restrict Direction of rotation in Realtime

have a hand of clock which the player once starts rotating (Dragging) CCW until he completes one full rotation. ( Without lifting drag )
I am trying to lock the rotation to only CCW direction while/once the player starts rotating. I got help from the following links : [Detect Direction][1] by #BobBobson108
Here is gif of what is actually happening: Demo
void OnMouseDrag()
{
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90f));
hand_vector = transform.up;
cross_product = Vector3.Cross(ref_vector, hand_vector);
dot_product = Vector3.Dot(cross_product, transform.forward*-1);
//Debug.Log("Hand Vector: " + hand_vector);
//Debug.Log("Ref Vector: " + ref_vector);
Debug.Log(cross_product);
Debug.Log(dot_product);
}
I tried to debug the values of the cross product, but the direction of resultant vector seems to be same even when when the player starts backward rotation.
Also the cross product vector changes direction only when the player starts rotation in CW direction from the default position i.e. 12 'o clock.
I have very less experience of working with Quaternions and rotations. Any help will be highly helpful. Thanks !!!
Desired
Use http://docs.unity3d.com/ScriptReference/Vector3.Angle.html function to get the value of the angle. You should get a positive/negative value depending on the direction.
You could then lock the rotation if the sign is not the correct direction you want.
I managed to figure out a solution to above question. Here I am posting my solution in case anyone stumbles across a similar situation in future.
void OnMouseDrag()
{
transform.Rotate(new Vector3(0,0, Mathf.Sqrt(Input.GetAxis("Mouse X") * Input.GetAxis("Mouse X") + Input.GetAxis("Mouse Y") * Input.GetAxis("Mouse Y"))));
/*
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
print("Angle is :");
print(Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90f);
if (counter_clockwise)
{
if (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90f > 0)
{
angle = Mathf.Max(angle, Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90);
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
if (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90f < 0)
{
angle = Mathf.Max(angle, 360 + (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90));
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}
*/
glow_color.a = 1f;
child_yellowglow.color = glow_color;
}
I am setting the direction bools from using this :
void OnMouseDown()
{
Vector3 mouseDragStartPos = Input.mousePosition;
print("Mouse pos: " + mouseDragStartPos);
print(" ref_vector" + ref_vector);
if (mouseDragStartPos.x < ref_vector.x)
{
clockwise = false;
counter_clockwise = true;
print("Left !!!");
}
if (mouseDragStartPos.x >= ref_vector.x)
{
clockwise = true;
counter_clockwise = false;
print("Right !!!");
}
}
Hope this helps !!!

Trouble with raycasting in Unity 3

I'm trying to script my AI on a simple way. The AI does a raycast in front, left and right of it. Then it takes a random direction in a way that doesn't contain a "Boundary"-element.
First, my Update() checks if it's time to calculate a new direction. If it is, it calculates the new direction, then it moves to that.
I'm using the following code to move:
Debug.DrawLine(transform.position, transform.position + transform.forward, Color.yellow);
Debug.DrawLine(transform.position, transform.position + transform.right, Color.yellow);
Debug.DrawLine(transform.position, transform.position - transform.right, Color.yellow);
//DEBUGS START AND END POSITION ARE CORRECT
var startTime;
if (Time.time > nextUpdate) {
Debug.Log("New check");
var dirWay = MoveDirection();
//if (dirWay == 0)
//rot = Quaternion.Euler(0, 0, 0);
if (dirWay == 1) {
rot = Quaternion.Euler(0, 90, 0);
}
if (dirWay == 2) {
rot = Quaternion.Euler(0, -90, 0);
}
if (dirWay == 3) { //backwards
rot = Quaternion.Euler(0, 180, 0);
}
nextUpdate = Time.time + walkTime; //for example, 2: Every 2 seconds an update
direction.y = 1;
direction.y = 1.5 - transform.position.y;
transform.rotation = transform.rotation * rot;
transform.position = transform.position + transform.forward + transform.forward;
//Plus 2 * transform.forward because it moves 2 places
}
The function MoveDirection checks for obstacles through raycasting. My AI moves the correct distance in the correct time, but walks through walls. That means my raycasting is wrong. I'm using the following code:
var obstacles = ["Border", "Boundary", "BoundaryFlame"];
var frontAvailable = true;
var leftAvailable = true;
var rightAvailable = true;
var hitFront: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9)) {
for (var i = 0; i < obstacles.length; i++)
{
if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) > -1)
{
frontAvailable = false;
}
}
}
var hitLeft: RaycastHit;
if (Physics.Raycast(transform.position, transform.position - transform.right, hitLeft, 1.9)) {
for (var j = 0; j < obstacles.length; j++)
{
if (hitLeft.collider.gameObject.name.IndexOf(obstacles[j]) > -1)
{
leftAvailable = false;
}
}
}
var hitRight: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.right, hitRight, 1.9)) {
for (var k = 0; k < obstacles.length; k++)
{
if (hitRight.collider.gameObject.name.IndexOf(obstacles[k]) > -1)
{
rightAvailable = false;
}
}
}
So, am I right that when I want to check 2 units in front of the AI (transform.forward from the AI's point of view, not the global view!), I should use: Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9) ?
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
So, am I right that when I want to check 2 units in front of the AI
(transform.forward from the AI's point of view, not the global view!),
I should use: Physics.Raycast(transform.position, transform.position +
transform.forward, hitFront, 1.9) ?
you should have Physics.Raycast(transform.position,transform.forward,hitFront, 1.9);