How can I make the object to rotate back when releasing a key press? - c#

private void Update()
{
if (Input.GetKey(KeyCode.R))
{
objectToScale.transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
}
if (Input.GetKeyUp(KeyCode.R))
{
var lookPos = transform.position + objectToScale.transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, Time.deltaTime * damping);
}
}
I want that when I release the R key that the object will rotate facing another object.
It will work if I will remove the rotating back part out of the GetKeyUp. But then it will keep rotating the object all the time and I want it to rotate back only when I release the R key but it's not working.
This is what I mean the rotating back part:
if (Input.GetKeyUp(KeyCode.R))
{
var lookPos = transform.position + objectToScale.transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
objectToScale.transform.rotation = Quaternion.Slerp(objectToScale.transform.rotation, rotation, Time.deltaTime * damping);
}
But nothing happen when I release the R key.

Not the most elegant solution but it should illustrate my point.
If you wanted to lerp back to the original position, it needs to be called per update. So i just used a trigger.
You also need to minus the positions between the target and source to get the direction between each of them.
public bool freeRotateTrigger = false;
public Transform target;
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
freeRotateTrigger = true;
if (Input.GetKeyUp(KeyCode.R))
freeRotateTrigger = false;
if (freeRotateTrigger)
{
transform.Rotate(Vector3.up, 50f * Time.deltaTime);
}
else
{
Vector3 relativePos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 10f);
}
}

Related

Camera Behaviour

This camera script is intended to rotate and look at the player while it is moving and snapping to the player slowly while it isn't. (Player passes over a Vector3 beforeMoving before it is starting movement). My issue is I want to "feed" the deltaPosition slowly so it isn't a sudden snap but rather a slow and smooth transition, also stop adding if arrived.
private void LateUpdate()
{
if (player.isMoving)
{
desiredPosition = player.beforeMoving + offset;
}
else
{
Vector3 deltaPosition = player.transform.position - player.beforeMoving;
desiredPosition += deltaPosition * Time.deltaTime;
}
Quaternion camTurnAngle =
Quaternion.AngleAxis(input * rotationSpeed, Vector3.up);
desiredPosition = camTurnAngle * desiredPosition;
transform.position = Vector3.Slerp(transform.position, desiredPosition, smoothFactor);
transform.LookAt(player.transform);
}
Edit: I thought I would share the final code.
private void LateUpdate()
{
Quaternion rotation = Quaternion.Euler(GetDegree(), input.x * rotationSpeed, 0f);
if (player.isMoving)
{
desiredPosition = player.beforeMoving + offset;
CalculatePanTime();
}
else if (!player.isMoving)
{
desiredPosition = player.transform.position + offset;
}
transform.position = Vector3.Slerp(transform.position, rotation * desiredPosition, GetSpeed());
transform.LookAt(player.transform);
}
private void CalculatePanTime()
{
stoppedTime = Time.time;
playerDelta = Vector3.Distance(player.transform.position, player.beforeMoving);
timeToPan = (playerDelta / snappingSpeed) * Time.deltaTime;
}
private float GetSpeed()
{
if (Time.time < stoppedTime + timeToPan)
{
controlsDisabled = true; return snappingSpeed;
}
else
{
controlsDisabled = false; return smoothSpeed;
}
}
You are telling us what you want the code to do, that is good. You are also posting the code you implemented to achieve your goal, that is also good. Can you also tell us what is not working as you want it to work as a result of that code?
From what I understand it is the "Vector3 deltaPosition = player.transform.position - player.beforeMoving;
desiredPosition += deltaPosition * Time.deltaTime; " that is not behaving as you expect it to
maybe try something like this:
private void LateUpdate()
{
// snap the rotation center slowly to the player's position if not moving, or player's position before he started moving
desiredPosition = Vector3.Lerp(desiredPosition, player.beforeMoving, 0.1f);
// rotate around rotation center
Quaternion camTurnAngle = Quaternion.AngleAxis(rotationSpeed * Time.time, Vector3.up);
desiredPosition += camTurnAngle * offset;
// set the position to rotate around rotation center, and look towards player
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothFactor);
transform.LookAt(player.transform);
}
the problem with your code is that you are overshooting. If you want to implement something that decides how fast the camera snaps, or how much time it should take to snap, try introducing a float player.timeStopMoving = Time.time that you can use to correctly compute the position correction while he is not moving.
if(player.isMoving)
{
desiredPosition = player.beforeMoving;
}
else
{
const float timeNeededToSnap = 2f;
// or float timeNeededToSnap = (player.transform.position - player.beforeMoving).magnitude; // (which you could compute only once in player script, when he stops moving, and then reuse the value instead of performing a ".magnitude" every frame)
if(Time.time < player.timeStopMoving + timeNeededToSnap)
{
desiredPosition = Vector3.Lerp(desiredPosition, player.transform.position, (Time.time - player.timeStopMoving) / timeNeededToSnap);
}
else
{
// an other problem here is: if the player starts moving AGAIN before your desiredPosition got the value, do you want desired position to glich to the new player.beforeMoving?...
desiredPosition = player.transform.position;
}
}
EDIT:
To make he lerp less linear, you can use:
if(Time.time < player.timeStopMoving + timeNeededToSnap)
{
var t = Mathf.Cos(Maths.Pi * 0.5f * (Time.time - player.timeStopMoving) / timeNeededToSnap); // as timeDelta/totalTime goes from 0->1, multiply it by Pi/2 and the Cos will also go from 0->1 but with a smoothing speed
desiredPosition = Vector3.Lerp(desiredPosition, player.transform.position, t);
}

Unity 2d - I want to remove the rotation of the image in a top down game

I want to fix the code
So the image doesn't spin when translating to the next point
I tried to fix it, but could not
Apologized to the mistakes of writing, I do not speak English
enter image description here
[SerializeField]
private Transform[] patrolPoints;
Transform currentPatrolPoint;
int currentPatrolIndex;
protected override void Start()
{
currentPatrolIndex = 0;
currentPatrolPoint = patrolPoints[currentPatrolIndex];
base.Start();
}
protected override void Update()
{
transform.Translate(Vector3.up * Time.deltaTime * MySpeed);
if (Vector3.Distance(transform.position,currentPatrolPoint.position)<0.1f)
{
if(currentPatrolIndex+1<patrolPoints.Length)
{
currentPatrolIndex++;
}
else
{
currentPatrolIndex = 0;
}
currentPatrolPoint = patrolPoints[currentPatrolIndex];
}
Vector3 patrolPointDirection = currentPatrolPoint.position - transform.position;
float angle = Mathf.Atan2(patrolPointDirection.y, patrolPointDirection.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q,180f);
}
If you don't want it to rotate, remove this part, which is rotating the GameObject:
float angle = Mathf.Atan2(patrolPointDirection.y, patrolPointDirection.x) * Mathf.Rad2Deg - 90f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q,180f);
Also any variable that you are using just for the transform.rotation can be removed from your code.

How can i rotate a spaceship back to it's original rotation to face the original position?

The spaceship start moving from point A. The spaceship is facing the moving direction.
Now when i click one on the L key i want that the spaceship will rotate and will face to the original position it was start moving from. But even if the spaceship is now rotated by axis Z or Y or X to rotate it first to the regular rotation values and to face to the start moving position.
using UnityEngine;
using System.Collections;
public class Control : MonoBehaviour
{
public int rotationSpeed = 75;
public int movementspeed = 10;
public int thrust = 10;
private bool isPKeyDown = false;
private float acceleration = .0f;
private Vector3 previousPosition = Vector3.zero;
private Rigidbody _rigidbody;
private Vector3 originalPosition;
private Quaternion originalRotation;
// Use this for initialization
void Start()
{
originalPosition = transform.position;
originalRotation = transform.rotation;
_rigidbody = GetComponent<Rigidbody>();
Debug.Log("Acc Speed: " + thrust);
}
// Update is called once per frame
void Update()
{
var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f);
transform.Rotate(v3 * rotationSpeed * Time.deltaTime);
transform.position += transform.forward * Time.deltaTime * movementspeed;
if (Input.GetKey(KeyCode.Z))
transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
if (Input.GetKey("p"))
{
isPKeyDown = Input.GetKey("p");
float distance = Vector3.Distance(previousPosition, transform.position);
acceleration = distance / Mathf.Pow(Time.deltaTime, 2);
previousPosition = transform.position;
_rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration);
}
if (Input.GetKey("l"))
{
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, 0);
//StartCoroutine(TurnShip(transform, transform., originalRotation.eulerAngles, 1));
//transform.position += transform.forward * Time.deltaTime * movementspeed;
}
}
IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth)
{
float lerpSpeed = 0;
while (lerpSpeed < 1)
{
ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed);
lerpSpeed += Time.deltaTime * smooth;
yield return null;
}
}
void OnGUI()
{
if (isPKeyDown)
{
GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration);
}
}
}
This is where i click the L button but i tried some things but can't yet find how to do it.
The main goal is if i click once on L the spaceship should automatic rotate if needed and move back to the original position and then land on ground. L stand for landing that's the main goal.
Add a variable on top -
...
private Vector3 originalPosition;
private Quaternion originalRotation;
private bool landShip = false;
...
And use following code in update function -
if (Input.GetKey("l"))
{
landShip = true;
//StartCoroutine(TurnShip(transform, transform., originalRotation.eulerAngles, 1));
//transform.position += transform.forward * Time.deltaTime * movementspeed;
}
if(landShip){
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, 0.5f);
}
Once the spaceship lands, set the landShip value back to false.

How to stop rotation by LerpAngle after -90°

I have colliders where i can turn to the sides and i want camera to rotate as a player turn to the side. I'm using Mathf.LerpAngle for that, but when I press the key for turn to side, camera is rotating in loop. How can I make rotation stop?
The problem is that everytime I turn player should go -90 degrees to the left +90 to the right and there will be more turns so i can't use functions for setting rotation.
I was already trying to make it stop by that if statement with (lAngle > 90f)
float lAngle = Mathf.LerpAngle(minAngle, lMaxAngle, Time.deltaTime);
float rAngle = Mathf.LerpAngle(minAngle, rMaxAngle, Time.deltaTime);
Quaternion leftRotation = Quaternion.Euler(new Vector3 (0, lAngle, 0));
Quaternion rightRotation = Quaternion.Euler(new Vector3 (0, rAngle, 0));
transform.position = player.transform.position + offSet;
transform.LookAt (player.transform);
if (Input.GetKeyDown (KeyCode.LeftArrow) && GameObject.Find("Player").GetComponent<PlayerMovement>().turn) {
turnLeft = true;
} else if (Input.GetKeyDown (KeyCode.RightArrow) && GameObject.Find("Player").GetComponent<PlayerMovement>().turn) {
turnRight = true;
}
if(turnLeft) {
offSet = leftRotation * offSet;
transform.position = player.transform.position + offSet;
transform.LookAt (player.transform);
if (lAngle > 90f)
turnLeft = false;
}
if(turnRight) {
offSet = rightRotation * offSet;
transform.position = player.transform.position + offSet;
transform.LookAt (player.transform);
if (rAngle < -90f)
turnRight = false;
}
An alternative would be to use Quaternion.RotateTowards, as this allows rotations to negative values without the GameObject infinitely rotating (as it can't approach a negative value). Below you can see I'm storing the initial value of the GameObject's rotation, as a Vector3, before modifying this value whenever A or D keys are pressed. The rotation of the transform is then set to the result of Quaternion.RotateTowards.
public class FixedRotate : MonoBehaviour
{
[SerializeField]
private float m_rotationAngle;
[SerializeField]
private float m_rotationSpeed;
private Vector3 m_targetRotation;
public void Start()
{
m_targetRotation = transform.eulerAngles;
}
public void Update()
{
//Left
if(Input.GetKeyDown(KeyCode.A))
m_targetRotation.y -= m_rotationAngle;
//Right
if (Input.GetKeyDown(KeyCode.D))
m_targetRotation.y += m_rotationAngle;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(m_targetRotation), m_rotationSpeed);
}
}
The unfortunate thing about this approach is that you can't specify the amount of time a rotation takes, only the incremental value of which the object will rotate by. An alternative approach would be to use Mathf.Clamp to add Time.deltaTime / rotationTime to the existing y rotation of the GameObject and stop once it's reached +/- 90. Hope this helps.

Use Lerp Position and Slerp Rotation together (Unity)

This is what I try to do, When I click on a UI Element, the camera smoothly rotate (to look at the target) and simultaneously move on top of the target.
To perform that I use a two Coroutine one for the Lerp Position and the other one for the Slerp Rotation.
The issue is that the rotation doesn't work correctly, normally the camera should look down to see the top of the target but instead of doing this it look like the Camera first look at the target and after that move to his position.
I hope this is understandable ;)
Here is the code c#
Vector3 LocationProjectForPos = new Vector3(Loc_X, 100, Loc_Z);
Vector3 LocationProjectForRot = new Vector3(Loc_X, Loc_Y, Loc_Z);
Vector3 MainCameraPos = MainCamera.transform.position;
if(!IsCameraMoving & LocationProjectForPos != MainCameraPos)
{
StartCoroutine (CoroutineMovePositionCamera(LocationProjectForPos));
StartCoroutine (CoroutineMoveRotationCamera(LocationProjectForRot));
}
}
Moving the position of the Camera with Lerp
public IEnumerator CoroutineMovePositionCamera(Vector3 LocationProject)
{
float lerpTime = 5f;
float currentLerpTime = 0f;
IsCameraMoving = true;
Vector3 startPos = MainCamera.transform.localPosition;
Vector3 endPos = LocationProject;
while (lerpTime > 0)
{
lerpTime -= Time.deltaTime;
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime)
{
currentLerpTime = lerpTime;
}
float t = currentLerpTime / lerpTime;
t = t*t*t * (t * (6f*t - 15f) + 10f);
//t = t*t * (3f - 2f*t);
//t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
MainCamera.transform.localPosition = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
IsCameraMoving = false;
}
Rotate the Camera with Slerp
public IEnumerator CoroutineMoveRotationCamera(Vector3 LocationProject)
{
float lerpTime = 5f;
float currentLerpTime = 0f;
IsCameraMoving = true;
Vector3 relativePos = LocationProject - MainCamera.transform.localPosition;
Quaternion rotation = Quaternion.LookRotation(relativePos);
Quaternion current = MainCamera.transform.localRotation;
while (lerpTime > 0)
{
lerpTime -= Time.deltaTime;
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime)
{
currentLerpTime = lerpTime;
}
float t = currentLerpTime / lerpTime;
t = t*t*t * (t * (6f*t - 15f) + 10f);
//t = t*t * (3f - 2f*t);
//t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
MainCamera.transform.localRotation = Quaternion.Slerp(current, rotation, t);
yield return null;
}
IsCameraMoving = false;
}
Thanks for your help.
In CoroutineMoveRotationCamera, you need to update the relativePostion and rotation inside the while loop.
The relative position is changing while the camera is moving. Right now, your camera rotates based on the vector snapshot you took before the while loop started.
Add the following code after yield statement.
relativePos = LocationProject - Camera.main.transform.position;
rotation = Quaternion.LookRotation(relativePos);
before start coroutine, you should save the initial vars with member vars instead of function local vars

Categories