How to throw object to the direction the player is facing? - unity3d

I'm trying to throw an object after it is being held. Here is how the pickup works:
public void PickupObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
priorConstraints = pickupRB.constraints;
pickupRB.constraints = RigidbodyConstraints.FreezeAll;
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
pickupRB.isKinematic = true;
pickupRB.transform.parent = PickupParent.transform;
// pickupRB.isKinematic = true;
StartCoroutine(physicsObject.PickUp());
}
in the update():
if (currentlyPickedUpObject != null)
{
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
Debug.Log("Object is being thrown");
pickupRB.AddForce(camerDirection * 100);
}
Throw = false;
}
as shown above I was trying to add force to the direction the player to be able to throw the item in that direction in this part:
Vector3 camerDirection = mainCamera.transform.forward;
if (Throw)
{
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
Debug.Log("Object is being thrown");
pickupRB.AddForce(camerDirection * 100);
}
Throw = false;
The line is printed in the console when the throw button is pressed but nothing happens. How to fix this?

I think you're throwing the object but you're not releasing it, so on the next frame the currentlyPickedUpObject is not null so you go right back to moving it with the PickupParent again. Try setting it to null inside your if (Throw) statement so you don't keep moving it:
if (currentlyPickedUpObject != null)
{
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
Debug.Log("Object is being thrown");
pickupRB.AddForce(camerDirection * 100);
currentlyPickedUpObject = null; // <--- NEW
}
Throw = false;
}

The problem is when you pick the object up you are setting the pickupRB kinematic with line
pickupRB.isKinematic = true
So to fix this, you should add
pickupRB.isKinematic = false
before you add force to the rigidbody.

Related

How to make crouch/uncrouch smooth?

I'm trying to make the crouching/standing-up smoother and slower, as currently crouching/uncrouching is so quick(I mean transition between crouching/uncrouching)
private void Start()
{
startYScale = transform.localScale.y;
mainCamera = Camera.main;
}
private void PlayerMovement()
{
currentSpeed = moveSpeed;
move = controls.Player.Movement.ReadValue<Vector2>();
movement = (move.y * transform.forward) + (move.x * transform.right);
if (isRunning)
{
//run = controls.Player.Run.ReadValue<float>();
currentSpeed = runSpeed;
Vector3 running = (move.y * transform.forward) + (move.x * transform.right);
controller.Move(running * currentSpeed * Time.deltaTime);
}
if (crouching)
{
currentSpeed = crouchSpeed;
isRunning = false;
transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
// controller.radius = 0.2f;
}
else
{
currentSpeed = moveSpeed;
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
}
controller.Move(movement * currentSpeed * Time.deltaTime);
}
How would I fix that? is there a way to make the transition slower and smoother? please help.
Edit:
I tried changing the position of the camera too when crouching to make the transition from standing up to crouching smoother.
private void FixedUpdate()
{
var desierdHeight = crouching ? crouchYScale : startYScale;
if (controller.height != desierdHeight)
{
AdjustHeight(desierdHeight);
/* var camPos = playerCamera.transform.position;
camPos.y = controller.height;
playerCamera.transform.position = camPos;*/
playerCamera.transform.localPosition = new Vector3(0,controller.height, 0);
}
}
private void AdjustHeight(float height)
{
float center = height / 2;
controller.height = Mathf.Lerp(controller.height, height, crouchSpeed);
controller.center = Vector3.Lerp(controller.center, new Vector3(0, center,0), crouchSpeed);
}
but the transition is still fast.

How to prevent picked up object from changing its scale?

I'm facing an issue when I pick up an object in my game. Whenever I pick up an object and look around while holding the object, it stretches based on the perspective. Here is an example of an object before and after picking up:
Before picking up:
After picking up:
How can I maintain the object's scale and prevent it from stretching?
public void PickupObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
/* priorConstraints = pickupRB.constraints; // <--- NEW
pickupRB.constraints = RigidbodyConstraints.FreezeAll; // <--- NEW*/
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
pickupRB.isKinematic = true;
pickupRB.transform.parent = PickupParent.transform;
// pickupRB.isKinematic = true;
// StartCoroutine(physicsObject.PickUp());
}
The following code snippet is in Update() :
if (currentlyPickedUpObject != null)
{
HoldingItemIcon.SetActive(true);
InteractIcon.SetActive(false);
CenterIcon.SetActive(false);
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
HoldingItemIcon.SetActive(false);
InteractIcon.SetActive(false);
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
pickupRB.AddForce(camerDirection * 500);
currentlyPickedUpObject = null;
pickupRB.transform.parent = null;
}
Throw = false;
}
public void BreakConnection()
{
pickupRB.isKinematic = false;
pickupRB.transform.parent = null;
pickupRB.constraints = priorConstraints;
// pickupRB.constraints = RigidbodyConstraints.None;
currentlyPickedUpObject = null;
lookObject = null;
physicsObject.pickedUp = false;
currentDist = 0;
pickupRB.useGravity = true;
}
pickupParent's lossy scale while an object is picked:
What you can do is have an empty gameObject as a child of your player. That object is going to be the pickableObjectsParent and give this parent object the scale values of 1/(player's scale).

Move two player objects simultaneously with Raycast

We have our player controller script setup and working for an individual player object, but when we want to add a second one, we are running into complications. I know we could potentially use a LayerMask to have the RayCast ignore a player object, but when that happens, both objects will try to move into the same space and cause problems. I'm stumped at this point.
Player Controller Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float speed;
bool isMoving;
float distance;
Vector3 endPos;
public Text parText;
private int par;
public static int moves;
bool upDetect;
bool downDetect;
bool rightDetect;
bool leftDetect;
Vector3 upLine;
Vector3 downLine;
Vector3 leftLine;
Vector3 rightLine;
public bool actionCheck;
Vector3 actionLine;
void Start()
{
isMoving = false;
par = Counter.levelPar;
endPos = transform.position;
//setPar();
}
private void FixedUpdate()
{
upLine = new Vector3(transform.position.x, transform.position.y, transform.position.z + 0.1f);
upDetect = Physics.Linecast(transform.position, upLine);
Debug.DrawLine(transform.position, upLine);
downLine = new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.1f);
downDetect = Physics.Linecast(transform.position, downLine);
Debug.DrawLine(transform.position, downLine);
leftLine = new Vector3(transform.position.x - 0.1f, transform.position.y, transform.position.z);
leftDetect = Physics.Linecast(transform.position, leftLine);
Debug.DrawLine(transform.position, leftLine);
rightLine = new Vector3(transform.position.x + 0.1f, transform.position.y, transform.position.z);
rightDetect = Physics.Linecast(transform.position, rightLine);
Debug.DrawLine(transform.position, rightLine);
// actionLine = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
// actionCheck = Physics.Linecast(transform.position, actionLine);
// Debug.DrawLine(transform.position, actionLine);
if (Input.GetKey("left") && isMoving == false && leftDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray leftRay = new Ray(transform.position, Vector3.left);
if (Physics.Raycast(leftRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
}
}
//countMove();
}
if (Input.GetKey("right") && isMoving == false && rightDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray rightRay = new Ray(transform.position, Vector3.right);
if (Physics.Raycast(rightRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
}
}
//countMove();
}
if (Input.GetKey("up") && isMoving == false && upDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray upRay = new Ray(transform.position, Vector3.forward);
if (Physics.Raycast(upRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
}
}
//countMove();
}
if (Input.GetKey("down") && isMoving == false && downDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray downRay = new Ray(transform.position, -Vector3.forward);
if (Physics.Raycast(downRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
}
}
//countMove();
}
distance = Vector3.Distance(transform.position, endPos);
//Debug.Log(distance);
//Debug.Log(rightDetect);
//Debug.Log(leftDetect);
//Debug.Log(upDetect);
//Debug.Log(downDetect);
if (distance < 0.01)
{
distance = 0;
}
if (distance > 0)
{
transform.position = Vector3.Lerp(
transform.position, endPos,
Time.deltaTime * speed / distance);
transform.rotation = Quaternion.identity;
}
else
{
isMoving = false;
endPos = transform.position;
}
//setPar();
}
/*void countMove()
{
moves++;
if (par > 0)
{
par--;
}
}
void setPar()
{
parText.text = "Par: " + par.ToString();
}*/
}
Figured it out. I had to use the Raycast hit to figure out what object it had hit. If the object had a "Player" tag, then it would go to that object's PlayerController script and get the value for it's end pos. Then I just had to adjust the endPos variable to compensate for the extra space. Here's my code.
PlayerController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float speed;
bool isMoving;
public float distance;
public Vector3 endPos;
public Text parText;
private int par;
public static int moves;
bool upDetect;
bool downDetect;
bool rightDetect;
bool leftDetect;
Vector3 upLine;
Vector3 downLine;
Vector3 leftLine;
Vector3 rightLine;
Vector3 actionLine;
Rigidbody rb;
void Start()
{
isMoving = false;
par = Counter.levelPar;
endPos = transform.position;
rb = GetComponent<Rigidbody>();
//setPar();
}
private void FixedUpdate()
{
upLine = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
upDetect = Physics.Linecast(transform.position, upLine);
Debug.DrawLine(transform.position, upLine);
downLine = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
downDetect = Physics.Linecast(transform.position, downLine);
Debug.DrawLine(transform.position, downLine);
leftLine = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
leftDetect = Physics.Linecast(transform.position, leftLine);
Debug.DrawLine(transform.position, leftLine);
rightLine = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
rightDetect = Physics.Linecast(transform.position, rightLine);
Debug.DrawLine(transform.position, rightLine);
// actionLine = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
// actionCheck = Physics.Linecast(transform.position, actionLine);
// Debug.DrawLine(transform.position, actionLine);
if (Input.GetKey("left") && isMoving == false && leftDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray leftRay = new Ray(transform.position, Vector3.left);
if (Physics.Raycast(leftRay, out hit))
{
if (hit.collider != null && hit.collider.tag != "Player")
{
endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
}
if (hit.collider.tag == "Player")
{
GameObject oPlayer = hit.collider.gameObject;
endPos = GetOtherEndPos(oPlayer);
endPos = new Vector3(endPos.x + 1, endPos.y, endPos.z);
}
}
//countMove();
}
if (Input.GetKey("right") && isMoving == false && rightDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray rightRay = new Ray(transform.position, Vector3.right);
if (Physics.Raycast(rightRay, out hit))
{
if (hit.collider != null && hit.collider.tag != "Player")
{
endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
}
if (hit.collider.tag == "Player")
{
GameObject oPlayer = hit.collider.gameObject;
endPos = GetOtherEndPos(oPlayer);
endPos = new Vector3(endPos.x - 1, endPos.y, endPos.z);
}
}
//countMove();
}
if (Input.GetKey("up") && isMoving == false && upDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray upRay = new Ray(transform.position, Vector3.forward);
if (Physics.Raycast(upRay, out hit))
{
if (hit.collider != null && hit.collider.tag != "Player")
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
}
if (hit.collider.tag == "Player")
{
GameObject oPlayer = hit.collider.gameObject;
endPos = GetOtherEndPos(oPlayer);
endPos = new Vector3(endPos.x , endPos.y, endPos.z - 1);
}
}
//countMove();
}
if (Input.GetKey("down") && isMoving == false && downDetect == false)
{
isMoving = true;
RaycastHit hit;
Ray downRay = new Ray(transform.position, -Vector3.forward);
if (Physics.Raycast(downRay, out hit))
{
if (hit.collider != null && hit.collider.tag != "Player")
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
}
if (hit.collider.tag == "Player")
{
GameObject oPlayer = hit.collider.gameObject;
endPos = GetOtherEndPos(oPlayer);
endPos = new Vector3(endPos.x, endPos.y, endPos.z + 1);
}
}
//countMove();
}
distance = Vector3.Distance(transform.position, endPos);
//Debug.Log(distance);
//Debug.Log("Name: " + this.name + " distance = " + distance + " vel = " + rb.velocity.magnitude + " isMoving: " + isMoving);
//Debug.Log(rightDetect);
//Debug.Log(leftDetect);
//Debug.Log(upDetect);
//Debug.Log(downDetect);
//Debug.Log(isMoving);
//Debug.Log("Velocity = " + rb.velocity.magnitude);
if (distance < 0.1)
{
distance = 0;
}
if (distance > 0)
{
transform.position = Vector3.Lerp(
transform.position, endPos,
Time.deltaTime * speed / distance);
transform.rotation = Quaternion.identity;
}
else
{
isMoving = false;
endPos = transform.position;
}
//setPar();
}
Vector3 GetOtherEndPos(GameObject oPlayer)
{
PlayerController script = oPlayer.GetComponent<PlayerController>();
return script.endPos;
}
/*void countMove()
{
moves++;
if (par > 0)
{
par--;
}
}
void setPar()
{
parText.text = "Par: " + par.ToString();
}*/
}

How to fix my game's jumping?

I wanted to create a game where I can move my player tile by tile. I have no idea how to make it jump and how to make it smooth.
This is my code so far:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
/*
* TILE BY TILE MOVEMENT
* */
private bool isMoving = false;
private bool isJumping = false;
private Vector3 targetPosition = new Vector3();
private Vector3 prevPosition = new Vector3();
public static bool canMove = true;
private Rigidbody2D rb2D;
void Start(){
prevPosition = transform.position;
rb2D = GetComponent<Rigidbody2D> ();
}
public void Move(string moveDirection){
if (isMoving == false && canMove == true) {
isMoving = true;
switch (moveDirection.ToLower()) {
case "left":
prevPosition = transform.position;
targetPosition = prevPosition + new Vector3(-2,0,0);
targetPosition.x = Mathf.Round(targetPosition.x);
break;
case "right":
prevPosition = transform.position;
targetPosition = prevPosition + new Vector3(2,0,0);
targetPosition.x = Mathf.Round(targetPosition.x);
break;
}
}
}
public void CancelMove(){
targetPosition = prevPosition;
Debug.Log("Canceled move");
}
void Update(){
if (isMoving == false) {
//Checks if there is ground
RaycastHit2D hit;
hit = Physics2D.Raycast (transform.position, -Vector2.up, 1.2f, 1 << LayerMask.NameToLayer("Ground"));
Debug.DrawRay (transform.position, -Vector2.up* 1.2f);
RaycastHit2D hitSideRight;
hitSideRight = Physics2D.Raycast (transform.position, Vector2.right, 1.2f, 1 << LayerMask.NameToLayer("Ground"));
Debug.DrawRay (transform.position, Vector2.right* 1.2f);
RaycastHit2D hitSideLeft;
hitSideLeft = Physics2D.Raycast (transform.position, -Vector2.right, 1.2f, 1 << LayerMask.NameToLayer("Ground"));
Debug.DrawRay (transform.position, -Vector2.right* 1.2f);
if((hitSideLeft.collider != null && hitSideLeft.collider.tag != "Box") || (hitSideRight.collider != null && hitSideRight.collider.tag != "Box")){
CancelMove();
Debug.Log("Can't move");
}
if (hit.collider != null || hit.collider == null){
if (Input.GetAxisRaw ("Horizontal") > 0 && hitSideRight.collider == null) {
Move ("right");
}
if (Input.GetAxisRaw ("Horizontal") < 0 && hitSideLeft.collider == null) {
Move ("left");
}
if(Input.GetKeyDown(KeyCode.Space)){
rb2D.AddForce(new Vector2(0,800));
}
}
} else {
if( transform.position.x != targetPosition.x )
{
// Move this object towards the target position
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5f);
}
else
{
isMoving = false;
}
}
}
}
This script is attached to the player. The player has a rigidbody2D and a boxCollider2D: http://i.stack.imgur.com/sEV5b.jpg

How can I make my character move only forward in Unity?

I have a character in Unity which use a move script, to transform its position.x, y, or z everytime when I press the appropriate button. Now I would like to change my code to just rotate the character when the user press the "a" or "d" and don't move it, however if the user press the "w" button move it forward.
I have already looked for it on the internet, and found out that I would need to use Vector3.forward somehow, but it doesn't work.
Here is my script:
This is the script which actually moves the character:
(It is attached to a Player object which contains the moveable characters)
public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;
Vector3 startPos;
Vector3 endPos;
bool firstInput;
public bool justJump;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
if (perc == 1) {
lerpTime = 1;
currentLerpTime = 0;
firstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
if (Input.GetButtonDown("right") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z);
}
if (Input.GetButtonDown("left") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z);
}
if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 0.5f);
}
if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.5f);
}
if (firstInput == true) {
currentLerpTime += Time.deltaTime * 5;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
if (perc > 0.8f) {
perc = 1;
}
if (Mathf.Round(perc) == 1) {
justJump = false;
}
}
}
}
And here is my "rotate script", which is attached to the moveable character(s) itself:
public class AnimationController : MonoBehaviour {
Animator anim;
public GameObject thePlayer;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Move moveScript = thePlayer.GetComponent<Move>();
if (moveScript.justJump == true) {
anim.SetBool("Jump", true);
}
else {
anim.SetBool("Jump", false);
}
if (Input.GetButtonDown("right")) {
gameObject.transform.rotation = Quaternion.Euler(0, 90, 0);
}
if (Input.GetButtonDown("left")) {
gameObject.transform.rotation = Quaternion.Euler(0, -90, 0);
}
if (Input.GetButtonDown("up")) {
gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
}
if (Input.GetButtonDown("down")) {
gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
}
}
}
Could you give me please any help on this? (Preferably with code, if you can :) )
Assuming you want your character to move to where it is facing, and trying not to do a lot of changes to your code, this is what could be done:
First, attach both your script directly to your movable character.
Then, change your Move script to:
public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;
Vector3 startPos;
Vector3 endPos;
bool firstInput;
public bool justJump;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
if (perc == 1) {
lerpTime = 1;
currentLerpTime = 0;
firstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
}
if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
}
if (firstInput == true) {
currentLerpTime += Time.deltaTime * 5;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
if (perc > 0.8f) {
perc = 1;
}
if (Mathf.Round(perc) == 1) {
justJump = false;
}
}
}
}
And your AnimationController script to
public class AnimationController : MonoBehaviour {
Animator anim;
public GameObject thePlayer;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Move moveScript = thePlayer.GetComponent<Move>();
if (moveScript.justJump == true) {
anim.SetBool("Jump", true);
}
else {
anim.SetBool("Jump", false);
}
if (Input.GetButtonDown("right")) {
gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
}
if (Input.GetButtonDown("left")) {
gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
}
}
}
This will make your character rotate only when you press the left and right buttons, and move only when you press the up and down button.
Althought this solves your problem, this is not the best way to write scripts for your character movement.
I recommend you read more about the state machine design patter. This book by Robert Nystrom has a chapter about it and is really easy to read. Also, its free to read online :)