Using Vector3.Lerp to get midpoint - c#

I have a ball and want the main camera to follow it around so I attached a script to the camera:
public class Tracker : MonoBehaviour
{
public GameObject target;
void Update()
{
this.transform.position = Vector3.Lerp(this.transform.position, target.transform.position, .5f);
}
}
Where the target is the ball gameobject.
I want the camera to have the same x and y coordinates as the ball but keep its original Z.
Not sure what to do, maybe there's a different way of approaching this?

You can create you movement Vector3 with a constructor
for ex:
Vector3 newPos = new Vector3(target.transform.position.x,target.transform.position.y, transform.position.z);
this.transform.position = Vector3.Lerp(this.transform.position, newPos, .5f);

Related

How do I Detect if the Mouse is Moving on Screen in Unity?

I am making a 360 camera movement for my game called PROTOTYPE. I need a function or something to detect if the mouse is moving and in which direction on the x axis it is, to set an offset for the Camera Smoothing script, but I don't know any. Could somebody plz help me? Here is the Camera Smoothing script if u need it:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
void FixedUpdate()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
transform.LookAt(target);
}
}
I suppose you want to move your camera with your mouse, 360° around your character and also that script is attached to your player.
In order to do that, your FixedUpdate should look like this:
void FixedUpdate()
{
float horizontalAxis = Input.GetAxis("Mouse X"); // Getting the current mouse axis (left-right)
float verticalAxis = Input.GetAxis("Mouse Y"); // Getting the current mouse axis (up-down)
transform.RotateAround(player.transform.position, -Vector3.up, horizontalAxis * smoothSpeed);
transform.RotateAround(Vector3.zero, transform.right, verticalAxis * smoothSpeed);
}

Projectiles spawning with incorrect trajectory

I wrote a script for gun and projectile but whenever I fire it only fires in the right direction when im pointing upwards, if i'm pointing to the sides it shoots downwards and if i'm pointing downwards it shoots up.
I've been trying to get a simple script that makes the gun rotate to face the mouse (works) and then to spawn bullets going in the firection of the gun, however, they spawn weirdly when not facing upwards, i've tried changing the object that has the script but nothing else since i'm not exactly sure what the error is.
Gun script:
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour
{
public float offset;
public GameObject projectile;
public Transform shotPoint;
private float timeBtwShots;
public float startTimeBtwShots;
private void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
if (timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
}
Projectile script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
public float pSpeed;
public float lifeTime;
public GameObject destroyEffect;
private void Start()
{
Invoke("DestroyProjectile", lifeTime);
}
private void Update()
{
transform.Translate(transform.up * pSpeed * Time.deltaTime);
}
void DestroyProjectile()
{
Destroy(gameObject);
}
}
I know that in the projectile script i change the transform.up inside the void update to transform.right or left the effect reverses and oly shoots correctly in the direction that I typed but I have no idead how to make it shoot properly in all directions.
I would hold a reference to the direction fired in the projectile and then move each frame based on that direction. So after we Instantiate the projectile, we calculate the direction Vector based on the gun/players rotation. The following code should do exactly what you are looking for, but is untested as I am currently using my phone! Hope this helps!
Inside Projectile.
public Vector3 directionFired;
Inside Gun.
GameObject proj = Instantiate(projectile, shotPoint.position, transform.rotation);
proj.directionFired = Quaternion.Euler(transform.rotation) * Vector3.forward;
timeBtwShots = startTimeBtwShots;
Inside Projectile Update
transform.Translate(directionFired * pSpeed * Time.deltaTime)

How to move the camera up the Y axis only when the player reaches the top 1/2 of the screen in Unity C#

This is for a 2D platform game.
I don't want the camera to move up the Y axis when the player jumps. I only want it to move when the player to the upper part of the screen so it can scroll up to vertical platforms and ladders.
Does anybody know what to enter in the code and the Unity editor so that can be done?
Here's the code I have so far in the camera script.
public class CameraControl : MonoBehaviour {
public GameObject target;
public float followAhead;
public float smoothing;
private Vector3 targetPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
targetPosition = new Vector3 (target.transform.position.x, transform.position.y, transform.position.z);
if (target.transform.localScale.x > 0f) {
targetPosition = new Vector3 (targetPosition.x + followAhead, targetPosition.y, targetPosition.z);
} else {
targetPosition = new Vector3 (targetPosition.x - followAhead, targetPosition.y, targetPosition.z);
}
transform.position = Vector3.Lerp (transform.position, targetPosition, smoothing * Time.deltaTime);
}
}
I guess you have a bool tied to the jump which triggers the jumping animation.
So, in the Update() of the camera you can do something like this:
void Update() {
// Update camera X position
if (isPlayerJumping) return;
// Update camera Y position
}
This way, you update the Y position of the camera only if the player isn't jumping, while still updating the X position in all cases (even while jumping).

Follow Player By Camera in 2D games

I used This code for my MainCamera for following the player in my 2d game in Unity5 :
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform target;
// Update is called once per frame
void Update ()
{
if (target)
{
Vector3 point = GetComponent<Camera>().WorldToViewportPoint(target.position);
Vector3 delta = target.position - GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
}
It work fine But player is in middle of screen allways . i wan player be in down of screen and my sprite for show Earth of my game will stick below the camera . i mean better in following pictures:
What I Want :
The Result :
You can add a vertical offset to the calculation. Just adding it to destination should do that I think.
Vector3 destination = ...
destination.y += someOffset;
transform.position = Vector3.SmoothDamp(...);
Otherwise you could also add an empty gameobject to the player gameobject and use that as your target.
One thing that you might need to consider is the resolution.

Rotate cameran around a gameobject in unity3d

I want to rotate camera around an fbx object when a key is being pressed using unity 3d.How it do? I tried some examples but its not working. First i create a game object and add main camera child of it.
public class CameraOrbit : MonoBehaviour
{
public Transform target;
public float speed = 1f;
private float distance;
private float currentAngle = 0;
void Start()
{
distance = (new Vector3(transform.position.x, 0, transform.position.z)).magnitude;
}
void Update()
{
currentAngle += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
Quaternion q = Quaternion.Euler(0, currentAngle, 0);
Vector3 direction = q * Vector3.forward;
transform.position = target.position - direction * distance + new Vector3(0, transform.position.y, 0);
transform.LookAt(target.position);
}
}
I dont have access to unity at the moment so i might have messed something up.
The idea is keep an angle that you change based on input. Create a Quaternion from the angle (the Quaternion say how to rotate a vector to a certain direction), then rotate a Vector to that direction. Starting from the targets position move in that direction a certain distance and then look at the targets position.
This only implements rotation around the y axis, if you want rotation around the x axis all you need is another angle variable and then change to this Quaternion.Euler(currentAngleX, currentAngleY, 0);

Categories