How to start Vector3.Lerp from middle point? - unity3d

I have to move object from -10 to +10 x position. But I want to start my object from zero x.point to move, how can I start my lerp at the middle position?
Edit2: Object should start at x = 0 position and move to +10 x position and go to -10 x position and again +10 x, -10 x like a loop.
Vector3 pos1, pos2, pos0, pos3;
void Start()
{
pos0 = transform.position;
pos1 = pos0 + new Vector3(10, 0, 0);
pos2 = pos0 - new Vector3(10, 0, 0);
pos3 = pos1;
}
float time = 0.5f;
void FixedUpdate()
{
time += Mathf.PingPong(Time.time * 0.5f, 1.0f);
transform.position = Vector3.Lerp(pos2, pos1, time);
}

From Unity API documentation at https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
public static Vector3 Lerp(Vector3 a, Vector3 b, float t);
When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the
point midway between a and b.
In this symmetrical situation where x = 0 is exactly between start and end point, you could just use lerp with t starting at t = 0.5. Perhaps something like this:
Vector3 pos1, pos2, pos0, pos3;
private float t;
void Start()
{
pos0 = transform.position;
pos1 = pos0 + new Vector3(10, 0, 0);
pos2 = pos0 - new Vector3(10, 0, 0);
pos3 = pos1;
t = 0.5
}
void FixedUpdate()
{
t += Mathf.PingPong(Time.deltaTime * 0.5f, 1.0f);
transform.position = Vector3.Lerp(pos2, pos1, t);
}
And as pointed out by #BugFinder, you should probably use Time.deltaTime instead of Time.time

Here is how I would approach it:
Vector3 ToPos;
Vector3 FromPos;
void Start()
{
ToPos = transform.position + new Vector3(10, 0, 0);
FromPos = transform.position - new Vector3(10, 0, 0);
}
// using Update since you are setting the transform.position directly
// Which makes me think you aren't worried about physics to much.
// Also FixedUpdate can run multiple times per frame.
void Update()
{
// Using distance so it doesnt matter if it's x only, y only z only or a combination
float range = Vector3.Distance(FromPos, ToPos);
float distanceTraveled = Vector3.Distance(FromPos, transform.position);
// Doing it this way so you character can start at anypoint in the transition...
float currentRatio = Mathf.Clamp01(distanceTraveled / range + Time.deltaTime);
transform.position = Vector3.Lerp(FromPos, ToPos, currentRatio);
if (Mathf.Approximately(currentRatio, 1.0f))
{
Vector3 tempSwitch = FromPos;
FromPos = ToPos;
ToPos = tempSwitch;
}
}

Related

Range bound x-axis movement

I am working in unity/C# and trying to make a function that keeps an character in certain x range (-3 to 3). Below isthe code I got to work. Is there a way to simplify it?
//function creation to limit movement in the x axis
float rangeBoundX(int upperBound, int lowerBound, Vector3 i, float horMoveSpe = 0)
{
//will change velocity to keep the x value in the desired range. - velocity to mvoe away from the upper bound and positive velocity goes away from the lowerBound.
if (i.x > upperBound)
{
horMoveSpe = -1;
}
else if (i.x < lowerBound)
{
horMoveSpe = 1;
}
return horMoveSpe;
}
'private void FixedUpdate()'
{
Vector3 enemyforwardMove = transform.forward * enemySpeed * Time.fixedDeltaTime;
Vector3 horizontalMove = transform.position;
magn = rangeBoundX(3, -3, horizontalMove, magn);
horizontalMove = transform.right * magn * freq;
enemyRB.MovePosition(enemyRB.position + enemyforwardMove + horizontalMove);
}
Yes you can use Math.Clamp to bound any value Like this.
void update(){
float xPos = Mathf.Clamp(transform.position.x, -3, 3);
transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
}
using this code you can bound the position of object horizontaly.
Yes, there is a way to simplify it. You can use Mathf.Clamp method https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
example usage: float xPos = Mathf.Clamp(xValue, xMin, xMax);

Momentum and Inertia in Unity3D

So, when my i let go off my keys the controller stops like it hits a wall, i tried changing that but all that changed is that now it gets flung into outer space every time i press a key:
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 newMovement = transform.right * x + transform.forward * z;
momentum = new Vector3(characterController.velocity.x, 0, characterController.velocity.z);
newMovement.y = 0;
if (!newMovement.normalized.Equals(momentum.normalized))
{
Debug.Log("new" + newMovement.normalized);
Debug.Log(momentum.normalized);
momentum = (momentum.magnitude - 2f) > 0 ? momentum.normalized * (momentum.magnitude - 2f) : Vector3.zero;
if (newMovement.x == momentum.x)
momentum.x = 0;
if (newMovement.z == momentum.z)
momentum.z = 0;
}
else
momentum = Vector3.zero;
characterController.Move((newMovement * speed + velocity + momentum) * Time.deltaTime);
Also for some reason even though sometimes both vectors are equal they pass through the if statement(i tried using !=)(both vectors are logged on the first 2 lines of the if statement)
Use https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html, it will gradually slow the movement to zero, depending on the value of smoothTime:
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
private Vector3 newMovement;
void Update()
{
newMovement = transform.right * x + transform.forward * z;
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}

Rotation around 2 axes include unwanted Rotations

I want to spin an object around x and rotate y axis to a direction.
Quaternion qr = Quaternion.Euler(transform.eulerAngles.x, 0,0);
transform.rotation = Quaternion.Lerp(transform.rotation,qr,Time.deltaTime*5);
transform.rotation = Quaternion.Euler(xRotation(xRotationSpeed),
transform.eulerAngles.y, 0);
x and y Rotations combined make weird rotations and sometimes even rotate z even though I have it set to 0.
Here is an example that yawing an object while rolling.
Notice that the rotation is applied from right to left.
const float ROLL_SPEED = 120f;
const float YAW_SCALE = 60;
float _lastYawBase;
void Update()
{
var yawBase = Input.GetAxis("Horizontal");
var tsf = transform;
tsf.localRotation =
// 4. apply new yaw
Quaternion.Euler(0, yawBase * YAW_SCALE, 0) *
// 3. roll
Quaternion.Euler(ROLL_SPEED * Time.deltaTime, 0f, 0f) *
// 2. revert last yaw
Quaternion.Euler(0, -_lastYawBase * YAW_SCALE, 0) *
// 1. current rotation
tsf.localRotation;
_lastYawBase = yawBase;
}

How to create Ofscreen Enemy indicator in Unity 3D?

I am working on creating an Ofscreen Enemy indicator using the tutorial mentioned on below link. However I can get the indicator to rotate to point to the enemy but the indicator does not move from end to end of screen.
http://gamedevelopment.tutsplus.com/tutorials/positioning-on-screen-indicators-to-point-to-off-screen-targets--gamedev-6644
This is the desired outcome:
Until now i have managed to figure out the below Please help.
var screenCenter:Vector3 = new Vector3(0.5, 0.5, 0f);
//Note coordinates are translated
//Make 00 the centre of the screen instead of bottom left
screenpos -= screenCenter;
//find angle from center of screen instead of bototom left
var angle:float = Mathf.Atan2(screenpos.y, screenpos.x);
angle -= 90 * Mathf.Deg2Rad;
var cos:float = Mathf.Cos(angle);
var sin:float = -Mathf.Cos(angle);
screenpos = screenCenter + new Vector3(sin*150, cos*150, 0);
//y=mx + b format
var m:float = cos/sin;
var ScreenBounds:Vector3 = screenCenter;// * 0.9f;
//Check up and down first
if(cos > 0){
screenpos = new Vector3(ScreenBounds.y/m, ScreenBounds.y, 0);
}else{//down
screenpos = new Vector3(-ScreenBounds.y/m, -ScreenBounds.y, 0);
}
//If out of bound then get point on appropriate side
if(screenpos.x > ScreenBounds.x){//Out of bound must be on right
screenpos = new Vector3(ScreenBounds.x, ScreenBounds.y*m, 0);
}else if(screenpos.x < ScreenBounds.x){//Out of bound must be on left
screenpos = new Vector3(-ScreenBounds.x, -ScreenBounds.y*m, 0);
}
//Remove the co ordinate translation
screenpos += screenCenter;
var DistanceIndicatorRectT = DistanceIndicator.GetComponent(RectTransform);
DistanceIndicatorRectT.localPosition = new Vector3(screenpos.x * scrWidth/2, screenpos.y * scrHeight/2, DistanceIndicatorRectT.localPosition.z * screenpos.z);
DistanceIndicator.transform.rotation = Quaternion.Euler(0, 0, angle*Mathf.Rad2Deg);
I did a bit of a different approach than you, what Carlos suggested but without using physics.
If "t" is your target, this way you can get it's position on screen in pixels (if it's off screen it just goes to negative values or values higher that width)
Vector3 targetPosOnScreen = Camera.main.WorldToScreenPoint (t.position);
And this function that return a bool whether the Vector3 (in pixels) is on screen
bool onScreen(Vector2 input){
return !(input.x > Screen.width || input.x < 0 || input.y > Screen.height || input.y < 0);
}
First thing we should do is check if the target is on screen, if it's not then proceed with code.
if (onScreen (targetPosOnScreen)) {
//Some code to destroy indicator or make it invisible
return;
}
Then a simple calculation of angle between center of screen and target.
Vector3 center = new Vector3 (Screen.width / 2f, Screen.height / 2f, 0);
float angle = Mathf.Atan2(targetPosOnScreen.y-center.y, targetPosOnScreen.x-center.x) * Mathf.Rad2Deg;
Next part of code determines where the object is compared to camera based on angle we just calculated.
float coef;
if (Screen.width > Screen.height)
coef = Screen.width / Screen.height;
else
coef = Screen.height / Screen.width;
float degreeRange = 360f / (coef + 1);
if(angle < 0) angle = angle + 360;
int edgeLine;
if(angle < degreeRange / 4f) edgeLine = 0;
else if (angle < 180 - degreeRange / 4f) edgeLine = 1;
else if (angle < 180 + degreeRange / 4f) edgeLine = 2;
else if (angle < 360 - degreeRange / 4f) edgeLine = 3;
else edgeLine = 0;
http://s23.postimg.org/ytpm82ad7/Untitled_1.png
Image represents what value "edgeLine" will have based on target position (red represents camera's view) and black lines division of space.
And then we have this code which sets Transform "t2" (indicator) to correct position and angle.
t2.position = Camera.main.ScreenToWorldPoint(intersect(edgeLine, center, targetPosOnScreen)+new Vector3(0,0,10));
t2.eulerAngles = new Vector3 (0, 0, angle);
Below we have function "intersect" which code is:
Vector3 intersect(int edgeLine, Vector3 line2point1, Vector3 line2point2){
float[] A1 = {-Screen.height, 0, Screen.height, 0};
float[] B1 = {0, -Screen.width, 0, Screen.width};
float[] C1 = {-Screen.width * Screen.height,-Screen.width * Screen.height,0, 0};
float A2 = line2point2.y - line2point1.y;
float B2 = line2point1.x - line2point2.x;
float C2 = A2 * line2point1.x + B2 * line2point1.y;
float det = A1[edgeLine] * B2 - A2 * B1[edgeLine];
return new Vector3 ((B2 * C1[edgeLine] - B1[edgeLine] * C2) / det, (A1[edgeLine] * C2 - A2 * C1[edgeLine]) / det, 0);
}
We send to this function index of which line of camera's view (rectangle) we need to check intersection with, and construct a line between center of screen and target position.
For better explanation of this function look here : https://www.topcoder.com/community/data-science/data-science-tutorials/geometry-concepts-line-intersection-and-its-applications/
I just modified values of A1, B1 and C1, each of them is now array of 4 and each values represents value needed for one line of camera's view (rectangle).
If you want to implement margins just change the pivot of indicator (put the actual sprite renderer as child and move it in local space as you want).
Next thing would be making this work for array of targets and putting all those targets in given array. Hope this helps and don't be too hard on me, it's my first time posting here :)
Create a rectangle box collider delimiting the borders of the screen and use Physics2D.Raycast in the direction of the enemy.
The point of collision will tell you where the green arrow needs to be drawn.
In the example above, there is an error with the definition of the angle of visibility of a straight rectangle.
private void SetIndicatorPosition(Indicator obj)
{
var target = obj.Target;
var indicator = obj.PointToTarget;
if (target == null)
{
indicator.SetActive(false);
return;
}
Vector3 targetPosOnScreen = cam.WorldToScreenPoint(target.transform.position);
if (onScreen(targetPosOnScreen))
{
indicator.SetActive(false);
return;
}
indicator.SetActive(true);
Vector3 center = new Vector3(Screen.width / 2f, Screen.height / 2f, 0);
float angle = Mathf.Atan2(targetPosOnScreen.y - center.y, targetPosOnScreen.x - center.x) * Mathf.Rad2Deg;
float scale;
if (Screen.width > Screen.height)
scale = Screen.width / Screen.height;
else
scale = Screen.height / Screen.width;
float degreeRange = 360f / (scale + 1);
float angle2 = Mathf.Atan2(Screen.height - center.y, Screen.width - center.x) * Mathf.Rad2Deg;
if (angle < 0) angle = angle + 360;
int edgeLine;
if (angle < angle2) edgeLine = 0;
else if (angle < 180 - angle2) edgeLine = 1;
else if (angle < 180 + angle2) edgeLine = 2;
else if (angle < 360 - angle2) edgeLine = 3;
else edgeLine = 0;
indicator.transform.position = Camera.main.ScreenToWorldPoint(Intersect(edgeLine, center, targetPosOnScreen));
indicator.transform.eulerAngles = new Vector3(0, 0, angle);
}
Vector3 Intersect(int edgeLine, Vector3 line2point1, Vector3 line2point2)
{
float[] A1 = { -Screen.height, 0, Screen.height, 0 };
float[] B1 = { 0, -Screen.width, 0, Screen.width };
float[] C1 = { -Screen.width * Screen.height, -Screen.width * Screen.height, 0, 0 };
float A2 = line2point2.y - line2point1.y;
float B2 = line2point1.x - line2point2.x;
float C2 = A2 * line2point1.x + B2 * line2point1.y;
float det = A1[edgeLine] * B2 - A2 * B1[edgeLine];
var x = (B2 * C1[edgeLine] - B1[edgeLine] * C2) / det;
var y = (A1[edgeLine] * C2 - A2 * C1[edgeLine]) / det;
return new Vector3(x, y, 0);
}
bool onScreen(Vector2 input)
{
return !(input.x > Screen.width || input.x < 0 || input.y > Screen.height || input.y < 0);
}
public class Indicator
{
public GameObject Target { get; private set; }
public GameObject PointToTarget { get; private set; }
public Indicator(GameObject target, GameObject pointToTarget, ObjectTypeEnum type)
{
Target = target;
PointToTarget = pointToTarget;
var texture = pointToTarget.GetComponentInChildren<UITexture>();
if (texture != null)
{
texture.color = Helper.GetHintColor(type);
}
}
}
You can call in update
foreach (var obj in listIndicator)
{
SetIndicatorPosition(obj);
}

trampoline unity code not working

So I'm trying to create a realistic trampoline jump instead of the player falling through the trampoline and then slingshotting back up, whilst instead allowing the player to instantly shoot upon contact with the trampoline and come down a relative gravity.
Where am I going wrong and what can I do to fix it?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class small_bounce_script: MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private Vector3 bounce = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
if (bounce.sqrMagnitude > 0) {
moveDirection = bounce;
bounce = Vector3.zero;
} else {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void OnTriggerEnter(Collider other) {
Debug.Log ("Controller collider hit");
Rigidbody body = other.attachedRigidbody;
// Only bounce on static objects...
if ((body == null || body.isKinematic) && other.gameObject.controller.velocity.y < -1f) {
float kr = 0.5f;
Vector3 v = other.gameObject.controller.velocity;
Vector3 n = other.normal;
Vector3 vn = Vector3.Dot(v,n) * n;
Vector3 vt = v - vn;
bounce = vt -(vn*kr);
}
}
}
A trampoline reacts like a spring device. Let's assume gravity is in Y direction and the trampoline surface is positionied in the X,Z plane.
Then your Y coordinate y is proportional to a sine function during OnTriggerStay. Velocity v in Y direction as 1st derivative of y is then a cosine function, while X and Z velocity remain constant.
y (t) = yMax * sin (f * t)
v (t) = yMax * f * cos (f * t)
Considering conservation of energy, we have:
E = 0.5 * m * vMax² = 0.5 * k * yMax²
=> yMax = ± SQRT (k / m) * vMax
vMax := speed in Y direction when hitting the trampoline. ± because for landing and starting
yMax := maximum amplitude when v == 0, i.e. hwo deep should the player sink before returning
k := spring constant defining trampoline behaviour i.e. how strong it is
m := player's mass
f := SQRT (k / m)
So all you need to do is playing around with the spring constant and have something like this in your Update method:
Vector3 velocity = rigidbody.velocity;
float elapsedTime = Time.time - timestampOnEnter;
velocity.y = YMax * FConst * Mathf.cos (FConst * elapsedTime);
rigidbody.velocity = velocity;
Member var timestampOnEnter is taken in OnTriggerEnter, FConst is the constant we called f in the maths part.