I want to flip my sprites depending on which direction they are going and then stay in that position until they move again. I've tried velocity detection and the flipping the sprites accordingly, but since my movement is not by force but by changing position it can't detect velocity (I think), so any suggestions would be helpful.
Here's the movement script I've got so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
//DRAG AND DROP
bool isBeingDragged = false;
//POSITIONS AND TARGET
public float speed = 0.5f;
float timer = 0f;
public float waitTime;
public float distanceToAchieve = 0.2f;
bool isFollowing = true;
bool isWaiting = false;
//GOAL TARGET
public GameObject goalGameObject;
Vector2 target;
public Vector2 bounds = new Vector2(1.75f, 3);
private Vector2 RP;
private float startPosX;
private float startPosY;
void Start()
{
if (this.gameObject.tag == "1")
{
transform.position = RandomizePosition();
}
target = RandomizePosition();
isFollowing = true;
}
private void Update()
{
if (!isBeingDragged)
{
if (!isWaiting && Vector2.Distance(transform.position, target) < distanceToAchieve)
{
isWaiting = true;
isFollowing = false;
RandomizeGoal();
}
if (isWaiting)
{
timer += Time.deltaTime;
if (timer > waitTime)
{
timer = 0f;
isWaiting = false;
isFollowing = true;
}
}
}
}
private void FixedUpdate()
{
if (isFollowing && !isBeingDragged)
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
if (isBeingDragged)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
}
}
Vector2 RandomizePosition()
{
RP = new Vector2(transform.position.x - Random.Range(-1f, 1f), transform.position.y - Random.Range(-1f, 1f));
if (RP.x < bounds.x && RP.x > bounds.x * -1 && RP.y < bounds.y && RP.y > bounds.y * -1)
{
return RP;
}
else
{
return new Vector2(transform.position.x, transform.position.y);
}
}
void RandomizeGoal()
{
waitTime = Random.Range(2, 10);
target = RandomizePosition();
goalGameObject.transform.position = target;
}
private void OnMouseDown()
{
isBeingDragged = true;
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
startPosX = mousePos.x - this.transform.localPosition.x;
startPosY = mousePos.y - this.transform.localPosition.y;
}
private void OnMouseUp()
{
isBeingDragged = false;
GetComponent<Merging>().CheckNearest();
}
}
private void FixedUpdate()
{
if (isFollowing && !isBeingDragged)
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
if (isBeingDragged)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
// capture the current position of target
// by the way, why you use "loclPosition" for moving ??
// I don't thinkg this is appropriate.
var currentPos = this.gameObject.transform.localPosition;
var newPos = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
// then compare this with new position
bool isLeftSided = false;
if (newPos.x > currentPos.x)
{
isLeftSided = true;
}
else
{
isLeftSided = false;
}
// And figure out the direction . in this example, I wrote just right-left case
// use the y pos for up and down
// then flip the sprite ( this case, default is right sided )
this.gameObject.GetComponent<SpriteRenderer>().flipX. = isLeftSided;
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, 0);
}
}
Related
I'm trying to make a First Person Controller and after I jump, right as the player is about to hit the ground, it jumps a second time by itself. This doesn't seem to be the case when the player lands on ground higher or lower than the ground it originally jumped on.
This is my Player code:
`
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[SerializeField] float mouseSensitivity = 3f;
[SerializeField] float walkingSpeed = 10f;
[SerializeField] float flyingSpeed = 15;
[SerializeField] float climbingSpeed = 5f;
[SerializeField] float mass = 1f;
[SerializeField] float acceleration = 20f;
[SerializeField] float worldBottomBoundary = -100f;
public Transform cameraTransform;
public bool IsGrounded => controller.isGrounded;
public float Height
{
get => controller.height;
set => controller.height = value;
}
public event Action OnBeforeMove;
public event Action<bool> OnGroundStateChange;
internal float movementSpeedMultiplier;
State _state;
public State CurrentState
{
get => _state;
set
{
_state = value;
velocity = Vector3.zero;
}
}
public enum State
{
Walking,
Flying,
Climbing
}
CharacterController controller;
internal Vector3 velocity;
Vector2 look;
(Vector3, Quaternion) initialPositionAndRotation;
bool wasGrounded;
PlayerInput playerInput;
InputAction moveAction;
InputAction lookAction;
InputAction flyUpDownAction;
void Awake()
{
controller = GetComponent<CharacterController>();
playerInput = GetComponent<PlayerInput>();
moveAction = playerInput.actions["move"];
lookAction = playerInput.actions["look"];
flyUpDownAction = playerInput.actions["flyUpDown"];
}
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
initialPositionAndRotation = (transform.position, transform.rotation);
}
public void Teleport(Vector3 position, Quaternion rotation)
{
transform.position = position;
Physics.SyncTransforms();
look.x = rotation.eulerAngles.y;
look.y = rotation.eulerAngles.z;
velocity = Vector3.zero;
}
void Update()
{
movementSpeedMultiplier = 1f;
switch (CurrentState)
{
case State.Walking:
UpdateGround();
UpdateGravity();
UpdateMovement();
UpdateLook();
CheckBounds();
break;
case State.Flying:
UpdateMovementFlying();
UpdateLook();
break;
case State.Climbing:
UpdateMovementClimbing();
UpdateLook();
break;
}
}
void CheckBounds()
{
if (transform.position.y < worldBottomBoundary)
{
var (position, rotation) = initialPositionAndRotation;
Teleport(position, rotation);
}
}
void UpdateGround()
{
if (wasGrounded != IsGrounded)
{
OnGroundStateChange?.Invoke(IsGrounded);
wasGrounded = IsGrounded;
}
}
void UpdateGravity()
{
var gravity = Physics.gravity * mass * Time.deltaTime;
velocity.y = IsGrounded ? -1f : velocity.y + gravity.y;
}
Vector3 GetMovementInput(float speed, bool horizontal = true)
{
var moveInput = moveAction.ReadValue<Vector2>();
var flyUpDownInput = flyUpDownAction.ReadValue<float>();
var input = new Vector3();
var referenceTransform = horizontal ? transform : cameraTransform;
input += referenceTransform.forward * moveInput.y;
input += referenceTransform.right * moveInput.x;
if (!horizontal)
{
input += transform.up * flyUpDownInput;
}
input = Vector3.ClampMagnitude(input, 1f);
input *= speed * movementSpeedMultiplier;
return input;
}
void UpdateMovement()
{
OnBeforeMove?.Invoke();
var input = GetMovementInput(walkingSpeed);
var factor = acceleration * Time.deltaTime;
velocity.x = Mathf.Lerp(velocity.x, input.x, factor);
velocity.z = Mathf.Lerp(velocity.z, input.z, factor);
controller.Move(velocity * Time.deltaTime);
}
void UpdateMovementFlying()
{
var input = GetMovementInput(flyingSpeed, false);
var factor = acceleration * Time.deltaTime;
velocity = Vector3.Lerp(velocity, input, factor);
controller.Move(velocity * Time.deltaTime);
}
void UpdateMovementClimbing()
{
var input = GetMovementInput(climbingSpeed, false);
var forwardInputFactor = Vector3.Dot(transform.forward, input.normalized);
if (forwardInputFactor > 0)
{
input.x = input.x * .5f;
input.z = input.z * .5f;
if (Mathf.Abs(input.y) > .2f)
{
input.y = Mathf.Sign(input.y) * climbingSpeed;
}
}
else
{
input.y = 0;
input.x = input.x * 3f;
input.z = input.z * 3f;
}
var factor = acceleration * Time.deltaTime;
velocity = Vector3.Lerp(velocity, input, factor);
controller.Move(velocity * Time.deltaTime);
}
void UpdateLook()
{
var lookInput = lookAction.ReadValue<Vector2>();
look.x += lookInput.x * mouseSensitivity;
look.y += lookInput.y * mouseSensitivity;
look.y = Mathf.Clamp(look.y, -90, 90f);
cameraTransform.localRotation = Quaternion.Euler(-look.y, 0, 0);
transform.localRotation = Quaternion.Euler(0, look.x, 0);
}
void OnToggleFlying()
{
CurrentState = CurrentState == State.Flying ? State.Walking : State.Flying;
}
}
`
And this is my PlayerJumping code:
`
using UnityEngine;
[RequireComponent(typeof(Player))]
public class PlayerJumping : MonoBehaviour
{
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float jumpPressBufferTime = .05f;
[SerializeField] float jumpGroundGraceTime = .2f;
[SerializeField] int maxJumps = 1;
Player player;
bool tryingToJump;
float lastJumpPressTime;
float lastGroundedTime;
int jumps;
void Awake()
{
player = GetComponent<Player>();
}
void OnEnable()
{
player.OnBeforeMove += OnBeforeMove;
player.OnGroundStateChange += OnGroundStateChange;
}
void OnDisable()
{
player.OnBeforeMove -= OnBeforeMove;
player.OnGroundStateChange -= OnGroundStateChange;
}
void OnJump()
{
tryingToJump = true;
lastJumpPressTime = Time.time;
}
void OnBeforeMove()
{
if (player.IsGrounded) jumps = 0;
var wasTryingToJump = Time.time - lastJumpPressTime < jumpPressBufferTime;
var wasGrounded = Time.time - lastGroundedTime < jumpGroundGraceTime;
var isOrWasTryingToJump = tryingToJump || (wasTryingToJump && player.IsGrounded);
var isOrWasGrounded = player.IsGrounded || wasGrounded;
var jumpAllowed = jumps < maxJumps;
if (
jumpAllowed && isOrWasTryingToJump && isOrWasGrounded
|| jumpAllowed && tryingToJump
)
{
player.velocity.y += jumpSpeed;
jumps++;
}
tryingToJump = false;
}
void OnGroundStateChange(bool isGrounded)
{
if (!isGrounded) lastGroundedTime = Time.time;
}
}
`
Although to be honest, I've checked a million times and I'm starting to believe the problem isn't in the code but in the inspector, but don't let that cloud your judgement because I can be pretty stupid at times. Do you have any ideas?
That the User Game View Panel
Here is an Editor view
That the GameObject Movement in all axis, I don't wants that behavior to move in 3-Directions, I just want it to move in either X-axis OR Z_axis
I'm making a 3d game, in which a user can move vehicles using touch-controls that will allows the user to clear the parking area. this script is allied on the camera. this game this is like a 3D_Traffic_Jam_Parking clearance... I'm confused. I just tried to go on multiple learning platforms, but don't get it...!!!
public class ZarSwipe2D_Control : MonoBehaviour
{
#region Variables
private float distance;
private bool isDraging = false;
private bool swipeLeft, swipeRight, swipeUp, swipeDown;
public Vector3 desiredPosition;
private Vector2 Swipe2D;
private Transform Player;
#region Getter-Setter
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
#endregion
#endregion
#region Controller Functionality
private void Update()
{
Vector3 v3;
float x = Swipe2D.x;
float y = Swipe2D.y;
Touch touch = Input.touches[0];
Vector3 pos = touch.position;
if (Input.touchCount != 1)
{
isDraging = false;
return;
}
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(pos);
if (Physics.Raycast(ray, out RaycastHit hit))
{
if (hit.collider.tag == "Player")
{
Player = hit.transform;
distance = hit.transform.position.z - pos.z;
v3 = new Vector3(pos.x, pos.y, distance);
v3 = Camera.main.ScreenToWorldPoint(v3);
desiredPosition = Player.position - v3;
isDraging = true;
}
}
}
if (isDraging && touch.phase == TouchPhase.Moved)
{
v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
v3 = Camera.main.ScreenToWorldPoint(v3);
Player.position = v3 + desiredPosition;
}
if (isDraging && (touch.phase == TouchPhase.Ended || touch.phase ==TouchPhase.Canceled))
{
isDraging = false;
}
}
#endregion
}
If you are using a rigidbody component there is an option to disable rotation or movement along 1 axis only
#region Variables
//private float dist; // for old Code
private bool dragging = false;
//private Vector3 offset; // for old Code
private Transform toDrag;
private Vector2 StartSwipePos, EndSwipePos;
#endregion
#region Swipe Controller - Axis Along X&Z
void Update()
{
//Vector3 v3;
if (Input.touchCount != 1)
{
dragging = false;
return;
}
Touch touch = Input.touches[0];
Vector3 pos = touch.position;
if (Input.touchCount == 1)
{
if (touch.phase == TouchPhase.Began)
{
StartSwipePos = touch.position;
dragging = true;
#region OldCode - Touch Begin
//Ray ray = Camera.main.ScreenPointToRay(pos);
//if (Physics.Raycast(ray, out RaycastHit hit))
//{
// if (hit.collider.CompareTag("Player"))
// {
// toDrag = hit.transform;
// dist = hit.transform.position.z - Camera.main.transform.position.z;
// v3 = new Vector3(pos.x, pos.y, dist);
// v3 = Camera.main.ScreenToWorldPoint(v3);
// offset = toDrag.position - v3;
// dragging = true;
// }
//}
#endregion
}
if (dragging && touch.phase == TouchPhase.Moved)
{
EndSwipePos = touch.position;
Ray ray = Camera.main.ScreenPointToRay(pos);
if (Physics.Raycast(ray, out RaycastHit hit))
{
Vector2 Distance = EndSwipePos - StartSwipePos;
float x_Distance = Mathf.Abs(Distance.x);
float Y_Distance = Mathf.Abs(Distance.y);
if (x_Distance > Y_Distance)
{
if (Distance.x > 0)
{
if (hit.collider.CompareTag("Player"))
{
toDrag = hit.transform;
toDrag.transform.Translate(25f * Time.deltaTime, 0, 0);
Debug.Log("Right");
}
}
if (Distance.x < 0)
{
if (hit.collider.CompareTag("Player"))
{
toDrag = hit.transform;
toDrag.transform.Translate(-25f * Time.deltaTime, 0, 0);
Debug.Log("Left");
}
}
}
if (Y_Distance > x_Distance)
{
if (Distance.y > 0)
{
if (hit.collider.CompareTag("Player"))
{
toDrag = hit.transform;
toDrag.transform.Translate(0, 0, 25f * Time.deltaTime);
Debug.Log("Up");
}
}
if (Distance.y < 0)
{
if (hit.collider.CompareTag("Player"))
{
toDrag = hit.transform;
toDrag.transform.Translate(0, 0, -25f * Time.deltaTime);
Debug.Log("Down");
}
}
}
}
#region Old Code - Touch Move
//v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
//v3 = Camera.main.ScreenToWorldPoint(v3);
//toDrag.position = v3 + offset;
#endregion
}
if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
{
dragging = false;
}
}
}
#endregion
My playable character turns left or right when it meets an obstacle with a collider. It's normal but I want to know if there is a way to disable it.
this is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour
{
public Vector3 startPosition;
private const float LANE_DISTANCE = 3.0f;
private const float TURN_SPEED = 0.5f;
//Functionality
private bool isRunning = false;
public bool isClimbing = false;
private readonly object down;
private CharacterController controller;
[SerializeField]
private float jumpForce = 5.0f;
private float verticalVelocity = 0.0f;
private float gravity = 10.0f;
//Speed
private float originalSpeed = 4.0f;
private float speed = 4.0f;
private float speedIncreaseLastTick;
private float speedIncreaseTime = 2.5f;
private float speedIncreaseAmount = 0.1f;
private float climbingSpeed = 1.0f;
private int desiredLane = 0; //0 = left, 1 = middle, 2 = right
private Animator anim;
// Start is called before the first frame update
void Start()
{
speed = originalSpeed;
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
transform.position = startPosition;
}
// Update is called once per frame
void Update()
{
if (isClimbing)
{
transform.Translate(Vector3.up * climbingSpeed * Time.deltaTime);
}
if (!isRunning)
return;
if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
{
speedIncreaseLastTick = Time.time;
speed += speedIncreaseAmount;
//GameManager.Instance.UpdateScores();
}
// Gather the inputs on wich lane we should be
if (MobileInput.Instance.SwipeLeft)
{
MoveLane(false);
}
if (MobileInput.Instance.SwipeRight)
{
MoveLane(true);
}
// Calculate where we should be horizontally
Vector3 targetPosition = transform.position.z * Vector3.forward;
int posX = Mathf.Abs(desiredLane);
if (desiredLane < 0)
targetPosition += Vector3.left * posX * LANE_DISTANCE;
else if (desiredLane > 0)
targetPosition += Vector3.right * posX * LANE_DISTANCE;
//Calculate move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
bool isGrounded = IsGrounded();
anim.SetBool("Grounded", isGrounded);
//Calculate y
if (isGrounded) //If grounded
{
verticalVelocity = -0.1f;
if (MobileInput.Instance.SwipeUp)
{
//Jump
anim.SetTrigger("Jump");
verticalVelocity = jumpForce;
}
else if (MobileInput.Instance.SwipeDown)
{
//Slide
StartSliding();
Invoke("StopSliding", 1.0f);
}
}
else
{
verticalVelocity -= (gravity * Time.deltaTime);
//Fast falling machanics
if (MobileInput.Instance.SwipeDown)
{
verticalVelocity = -jumpForce;
}
}
moveVector.y = verticalVelocity;
moveVector.z = speed;
//Move the character
controller.Move(moveVector * Time.deltaTime);
//Rotate the player where is going
Vector3 dir = controller.velocity;
if (dir!= Vector3.zero)
{
dir.y = 0;
transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
}
}
// This function (MoveLane) allows the player to move to the left and to the right
private void MoveLane(bool goingRight)
{
if (!goingRight)
{
desiredLane--;
if (desiredLane == -6)
desiredLane = -5;
}
if (goingRight)
{
desiredLane++;
if (desiredLane == 6)
desiredLane = 5;
}
/* We wan rewrite the above function like this below
desiredLane += (goingRight) ? 1 : -1;
Mathf.Clamp(desiredLane, -5, 5);
*/
}
private bool IsGrounded()
{
Ray groundRay = new Ray(new Vector3(controller.bounds.center.x, (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
controller.bounds.center.z), Vector3.down);
Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
return (Physics.Raycast(groundRay, 0.2f + 0.1f));
}
public void StartRunning ()
{
isRunning = true;
anim.SetTrigger("StartRunning");
}
private void StartSliding()
{
anim.SetBool("Sliding", true);
controller.height /= 2;
controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
}
private void StopSliding()
{
anim.SetBool("Sliding", false);
controller.height *= 2;
controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
}
private void Crash()
{
anim.SetTrigger("Death");
isRunning = false;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
switch(hit.gameObject.tag)
{
case "Obstacle":
Crash();
break;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ladder")
{
isRunning = false;
isClimbing = true;
anim.SetBool("ClimbingLadder", true);
}
else if (other.gameObject.tag == "LadderCol2")
{
isClimbing = false;
anim.SetBool("ClimbingLadder", false);
transform.Translate(Vector3.forward * 1);
isRunning = true;
}
}
}
I see the problem. It's from these lines
Vector3 dir = controller.velocity;
if (dir!= Vector3.zero)
{ dir.y = 0;
transform.forward =
Vector3.Lerp(transform.forward, dir,
TURN_SPEED);
}
I added them to rotate a bit the player when it turns left or right.
I am trying to make a player movement controller like subway surfer using Character Controller, every thing is working fine with keyboard but I am getting an issue in swipe. When I swipe its moving only one frame. And also I want player to go left and right while player in air(Jumping). Please help.
Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerControllerScript : MonoBehaviour
{
public float speed = 8.0F;
public float jumpSpeed = 16.0F;
public float gravity = 80.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 1;
public int lanesCount = 3;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = -2;
public float deadZone = 0.1f;
public float sideSpeed = 12;
private bool Right = false;
private bool Left = false;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
float input = Input.GetAxis("Horizontal");
if (controller.isGrounded) {
if (Mathf.Abs(input) > deadZone)
{
if (!didChangeLastFrame)
{
didChangeLastFrame = true;
laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
if (laneNumber < 0) laneNumber = 0;
else if (laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
}
else
{
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump") || SwipeManager.IsSwipingUp())
moveDirection.y = jumpSpeed;
}
}
if (Left)
moveDirection.x = -jumpSpeed;
if (Right)
moveDirection.x = jumpSpeed;
if (SwipeManager.IsSwipingLeft())
{
Left = true;
Right = false;
}
if (SwipeManager.IsSwipingRight())
{
Right = true;
Left = false;
}
Vector3 pos = transform.position;
pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
transform.position = pos;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
I think,
if (Input.GetButton("Jump") || SwipeManager.IsSwipingUp())
moveDirection.y = jumpSpeed;
must be outside the if(controller.isGrounded) block. So, you jump regardless of gravity.
This is the final code;
using UnityEngine;
using System.Collections;
public class PlayerControllerScript : MonoBehaviour
{
public float speed = 8.0F;
public float jumpSpeed = 16.0F;
public float gravity = 80.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 1;
public int lanesCount = 3;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = -2;
public float deadZone = 0.1f;
public float sideSpeed = 12;
private bool Right = false;
private bool Left = false;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
float input = Input.GetAxis("Horizontal");
if (controller.isGrounded) {
if (Mathf.Abs(input) > deadZone)
{
if (!didChangeLastFrame)
{
didChangeLastFrame = true;
laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
if (laneNumber < 0) laneNumber = 0;
else if (laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
}
else
{
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
}
if (Input.GetButton("Jump") || SwipeManager.IsSwipingUp())
moveDirection.y = jumpSpeed;
if (Left)
moveDirection.x = -jumpSpeed;
if (Right)
moveDirection.x = jumpSpeed;
if (SwipeManager.IsSwipingLeft())
{
Left = true;
Right = false;
}
if (SwipeManager.IsSwipingRight())
{
Right = true;
Left = false;
}
Vector3 pos = transform.position;
pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
transform.position = pos;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
what I'm trying to achieve, is that while the player holds the mouse button
on a tile (any grid element, verically or horizontally aligned with the player), the player will move towards that tile with the possible directions of left,right,up,down only.
currently my code doesn't work for while pressing the mouse button, I think it has something to do with the raycasting.
second thing I want to achieve is that while the player is moving in the grid, if the player decides to change direction, he will be able, no matter if it's just opposite direction, or if he decideds to take a sudden turn left/right.
(I managed to achieve it without using the isMoving boolean condition only in the opposite direction, but I added it because when the player clicked while moving it slowed him down)
right now no change in movement while moving is available.
using UnityEngine;
using Holoville.HOTween;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 100f;
private Vector3 startPos;
private Vector3 endPos;
private float startTime;
private float journeyLength;
private tile currentTile;
private tile tileToMove;
private float angleToTurn = 0f;
private bool isMoving = false;
public static Player use;
void Awake()
{
use = this;
}
void Start () {
startPos = endPos = transform.position;
tileToMove = currentTile = fieldGenerator.use.tilesList[0];
}
// Update is called once per frame
void Update () {
MovePlayer();
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
if (!startPos.Equals(endPos))
transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
if (transform.position == endPos)
{
isMoving = false;
}
}
void MovePlayer()
{
Ray ray;
RaycastHit hit;
if (Input.GetMouseButtonDown(0) && !isMoving)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
for (int i = 0; i < fieldGenerator.use.tilesList.Count; i++)
{
if (fieldGenerator.use.tilesList[i].selfObject.collider.Raycast(ray, out hit, float.PositiveInfinity))
{
if (fieldGenerator.use.tilesList[i].selfObject.transform.position.x < transform.position.x && fieldGenerator.use.tilesList[i].selfObject.transform.position.y == transform.position.y)
{
angleToTurn = -180f;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
}
else if (fieldGenerator.use.tilesList[i].selfObject.transform.position.x > transform.position.x && fieldGenerator.use.tilesList[i].selfObject.transform.position.y == transform.position.y)
{
angleToTurn = 0;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
}
else if (fieldGenerator.use.tilesList[i].selfObject.transform.position.y < transform.position.y && fieldGenerator.use.tilesList[i].selfObject.transform.position.x == transform.position.x)
{
angleToTurn = -90f;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
}
else if (fieldGenerator.use.tilesList[i].selfObject.transform.position.y > transform.position.y && fieldGenerator.use.tilesList[i].selfObject.transform.position.x == transform.position.x)
{
angleToTurn = 90f;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
}
startTime = Time.time;
startPos = transform.position;
endPos = tileToMove.selfObject.transform.position;
journeyLength = Vector3.Distance(startPos, endPos);
transform.eulerAngles = new Vector3(0f, 0f, angleToTurn);
}
}
}
}
}
Unsure on what you're doing with the tiles and raycasting but here's how i'd do it. Apologies for the formatting, it's just how I work.
EDIT: Okay, given you said 'grid' movement I assumed the tiles were equidistant from each other. GetMouseButtonDown changed to GetMouseButton as well. This will mean that a ray is shot every update which you may want to look into making more efficient.
using UnityEngine;
using Holoville.HOTween;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 100.0f;
private float delta = 0.0f;
private float distance = 1.0f;
private Vector3 startPos;
private Vector3 endPos;
private tile currentTile;
private tile tileToMove;
private float angleToTurn = 0f;
private bool isMoving = false;
public static Player use;
void Awake() {
use = this;
}
void Start () {
startPos = endPos = transform.position;
tileToMove = currentTile = fieldGenerator.use.tilesList[0];
}
// Update is called once per frame
void Update () {
GetInput();
MovePlayer();
}
void MovePlayer() {
if ( isMoving ) {
if ( delta < 1 ) {
// Distance independant movement.
delta += ( speed/distance ) * Time.deltaTime;
transform.position = Vector3.Lerp(startPos, endPos, delta);
} else {
isMoving = false;
}
}
}
void GetInput() {
Ray ray;
RaycastHit hit;
if ( Input.GetMouseButton(0) ) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
for (int i = 0; i < fieldGenerator.use.tilesList.Count; i++) {
if ( fieldGenerator.use.tilesList[i].selfObject.collider.Raycast( ray, out hit, float.PositiveInfinity ) ) {
if (fieldGenerator.use.tilesList[i].selfObject.transform.position.x < transform.position.x && fieldGenerator.use.tilesList[i].selfObject.transform.position.y == transform.position.y) {
angleToTurn = -180f;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
} else if (fieldGenerator.use.tilesList[i].selfObject.transform.position.x > transform.position.x && fieldGenerator.use.tilesList[i].selfObject.transform.position.y == transform.position.y) {
angleToTurn = 0;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
} else if (fieldGenerator.use.tilesList[i].selfObject.transform.position.y < transform.position.y && fieldGenerator.use.tilesList[i].selfObject.transform.position.x == transform.position.x) {
angleToTurn = -90f;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
} else if (fieldGenerator.use.tilesList[i].selfObject.transform.position.y > transform.position.y && fieldGenerator.use.tilesList[i].selfObject.transform.position.x == transform.position.x) {
angleToTurn = 90f;
tileToMove = fieldGenerator.use.tilesList[i];
isMoving = true;
}
isMoving = true;
delta = 0f;
startPos = transform.position;
endPos = tileToMove.selfObject.transform.position;
distance = Vector3.Distance( startPos, endPos );
transform.eulerAngles = new Vector3(0f, 0f, angleToTurn);
}
}
}
}
}