I have a script which limits camera movement to within a defined range. Unfortunately, it only works on the first camera, and there are up to four orthographic cameras in my game.
I have been unable to adapt the script to work with multiple cameras, so I figured that the next best this would be to translate the points from the four corners from the first camera's viewport to world position, then from the world position back to the viewport of cameras 2, 3 and 4. However this doesn't appear to be working.
SpriteRenderer spriteBounds = GameObject.Find("Map").GetComponentInChildren<SpriteRenderer>();
float leftBound =0;
float rightBound =0;
float bottomBound =0;
float topBound = 0;
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer1.orthographicSize;
float horzExtent = vertExtent * (Screen.width * (camPlayer1.rect.width * 2)) / Screen.height; //I guess the problem is here... but how do I fix this??
leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer1.transform.position.y, bottomBound, topBound), camPlayer1.transform.position.z);
}
if((trackPlayer2 == null))
{
camPlayer2.transform.position = this.transform.position;
}
else
{
leftBound = camPlayer1.ViewportToWorldPoint(new Vector3(leftBound, 0, 0)).x;
rightBound = camPlayer1.ViewportToWorldPoint(new Vector3(rightBound, 0, 0)).x;
bottomBound = camPlayer1.ViewportToWorldPoint(new Vector3(0, bottomBound, 0)).y;
topBound = camPlayer1.ViewportToWorldPoint(new Vector3(0, topBound, 0)).y;
leftBound = camPlayer2.WorldToViewportPoint(new Vector3(leftBound, 0, 0)).x;
rightBound = camPlayer2.WorldToViewportPoint(new Vector3(rightBound, 0, 0)).x;
bottomBound = camPlayer2.WorldToViewportPoint(new Vector3(0, bottomBound, 0)).y;
topBound = camPlayer2.WorldToViewportPoint(new Vector3(0, topBound, 0)).y;
camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer2.transform.position.y, topBound, bottomBound), camPlayer2.transform.position.z);
}
Related
I have created new Unity project, have added a cube into the center and now want to make player to be able to rotate camera around this cube by swipes and/or mouse drags.
Please, name simple steps to implement this or keywords to find an answer or where to read about it?
public Transform Target;
public float distance = 2.0f;
public float xSpeed = 20.0f;
public float ySpeed = 20.0f;
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
public float distanceMin = 10f;
public float distanceMax = 10f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
void Update()
{
if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + Target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
Taken from https://answers.unity.com/questions/1257281/how-to-rotate-camera-orbit-around-a-game-object-on.html
If you only want to rotate around a specific axis, for example around Y, you could basicly just do this
this.transform.RotateAround(Target.transform.position, Vector3.up, Input.GetAxis("Mouse X")*20.0f);
Create a new C# Script called "CameraRotate", open it and paste this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate : MonoBehaviour {
public Transform cube;
public float sensitivityX;
public float sensitivityY;
GameObject pivot;
void Start () {
pivot = new GameObject ("pivot");
pivot.transform.position = cube.position;
transform.SetParent (pivot.transform);
}
void Update () {
pivot.transform.eulerAngles += new Vector3 (Input.GetAxis ("Mouse Y") * -sensitivityX * Time.deltaTime, Input.GetAxis ("Mouse X") * sensitivityY * Time.deltaTime, 0);
}
}
Assign this script to your camera, then in Inspector Tab, with the camera selected, assign your Cube (drag) into the "cube" slot, and assign a value to the sensitivity fields (i used something about 500), press play and test.
What does this script do?
It creates a new GameObject at the center of the Cube to be a reference of rotation, it's called pivot. So it sets the Camera as a child of the pivot, and rotates the pivot according with your mouse axis.
I wrote this way:
float mouseX = -Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
float magnitude = transform.position.magnitude;
Vector3 mouseSwipe = new Vector3(mouseX, mouseY, 0);
Vector3 startPoint = new Vector3((float)camera.pixelWidth / 2, (float)camera.pixelHeight / 2, magnitude - 1);
Vector3 startPointWorld = camera.ScreenToViewportPoint(startPoint);
Vector3 endPointWord = camera.ScreenToViewportPoint(startPoint + mouseSwipe);
Vector3 mouseSwipeWord = endPointWord - startPointWorld;
float dragLat = mouseSwipeWord.y;
float dragLng = mouseSwipeWord.x;
Vector3 oldPosition = transform.position / magnitude;
float lat = Mathf.Asin(oldPosition.y);
float rsmall = Mathf.Acos(oldPosition.y);
float lng = Mathf.Atan2(oldPosition.z / rsmall, oldPosition.x / rsmall);
lat += dragLat * 10 * 2 * Mathf.PI;
if( lat*180/Mathf.PI > 80 )
{
lat = 80 * Mathf.PI / 180;
}
else if( lat*180/Mathf.PI < -80)
{
lat = -80 * Mathf.PI / 180;
}
lng += dragLng * 10 * 2 * Mathf.PI * 2;
float y = Mathf.Sin(lat);
rsmall = Mathf.Cos(lat);
float x = rsmall * Mathf.Cos(lng);
float z = rsmall * Mathf.Sin(lng);
Vector3 newPosition = new Vector3(x, y, z);
newPosition *= magnitude;
transform.position = newPosition;
LookAtTarget();
The goal was to simulate mouse is rotation object by drag.
I am creating a game with Unity and I have a math problem.
I have a sphere with a radius of 10 and center of (0, 0, 0).
I want the camera to move around that sphere, but I can't find anywhere a way to do what I want to.
I move the camera in the X axis and the Y axis (and therefore get a point outside of the sphere) and I want to set it's Z axis so the camera would be back on the sphere, I am using this equation: r^2 = x^2 + y^2 + z^2 => z^2 = r^2 - x^2 - y^2
But it doesn't work… Please help me
EDIT
This is my code (in c#):
private void OnMouseDrag()
{
var newX = mainCameraTransform.position.x + Input.GetAxis("Mouse X");
var newY = mainCameraTransform.position.y + Input.GetAxis("Mouse Y");
var maxDistance = 10.0f;
newX = Mathf.Clamp(newX, -maxDistance * 0.85f, maxDistance * 0.85f);
newY = Mathf.Clamp(newY, 1.0f * 0.85f, maxDistance * 0.85f);
var newZ = Mathf.Sqrt(Mathf.Abs(maxDistance * maxDistance - newX * newX - newY * newY));
mainCameraTransform.position = new Vector3(newX, newY, newZ);
mainCameraTransform.LookAt(Vector3.zero);
}
As you can see I used Clamp to keep the X and Y less then the radius but it didn't help…
This isn't tested, but it should be pretty close
private void OnMouseDrag(){
Vector3 newPos = mainCameraTransform.position;
newPos += mainCameraTransform.up * Input.GetAxis("Mouse Y");
newPos += mainCameraTransform.right * Input.GetAxis("Mouse X");
newPos = newPos.normalized * 10f;
mainCameraTransform.position = newPos;
mainCameraTransform.LookAt(Vector3.zero, mainCameraTransform.up);
}
You have to limit 2D coordinates by circle border
len = Mathf.Sqrt(newX * newX + newY * newY);
//perhaps you have Len or Hypot function in your Math library
if len > maxDistance then
newX = maxDistance * newX / len
newY = maxDistance * newY / len;
Drag and drop this script on the camera to orbit it around a target using the right mouse button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbitAroundObject : MonoBehaviour {
public Transform target;
public float distance = 10.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
public float zoomSpeed = 1;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start() {
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>()) {
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate() {
if (target) {
if (Input.GetMouseButton(1)) {
velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
//distance -= (Input.mouseScrollDelta.y*Time.deltaTime);
distance = Mathf.Lerp(distance, distance-(Input.mouseScrollDelta.y*zoomSpeed) , Time.deltaTime * smoothTime);
distance = Mathf.Clamp(distance, distanceMin, distanceMax);
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
//Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
public static float ClampAngle(float angle, float min, float max) {
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
I am developing endless runner game like subway surfer in Unity.
I want to move my player, smoothly on swipe left or right.
How can do it?
Here is my code:
using UnityEngine;
using System.Collections;
public class SwipeScript3 : MonoBehaviour {
private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
//Quaternion targetx = Quaternion.Euler(0, -3f, 0);
//Quaternion targety = Quaternion.Euler(0, 3f, 0);
void FixedUpdate()
{
foreach(Touch t in Input.touches)
{
if (t.phase == TouchPhase.Began)
{
initialTouch = t;
}
else if (t.phase == TouchPhase.Moved && !hasSwiped)
{
float deltaX = initialTouch.position.x - t.position.x;
float deltaY = initialTouch.position.y - t.position.y;
distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
if (distance > 50f)
{
if (swipedSideways && deltaX > 0) //swiped left
{
//transform.rotation = Quaternion.Slerp (transform.rotation, targetx, Time.deltaTime * 0.8f);
this.transform.Rotate(new Vector3(0, -3f, 0)*Time.deltaTime);
//transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*2f );
transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * 5f);
}
else if (swipedSideways && deltaX <= 0) //swiped right
{
//transform.rotation = Quaternion.Slerp (transform.rotation, targety, Time.deltaTime * 0.8f);
this.transform.Rotate(new Vector3(0, 3f, 0)*Time.deltaTime);
//transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * f);
transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*5f );
}
else if (!swipedSideways && deltaY > 0) //swiped down
{
//this.transform.Rotate(new Vector3(0, 2f, 0));
transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x,transform.position.y,transform.position.z-5f),Time.deltaTime*2f );
}
else if (!swipedSideways && deltaY <= 0) //swiped up
{
this.GetComponent<Rigidbody>().velocity = new Vector3(this.GetComponent<Rigidbody>().velocity.x, 0, this.GetComponent<Rigidbody>().velocity.z);
this.GetComponent<Rigidbody>().AddForce(new Vector3(0, 400f, 0));
Debug.Log ("Swiped Up");
}
hasSwiped = true;
}
}
else if (t.phase == TouchPhase.Ended)
{
initialTouch = new Touch();
hasSwiped = false;
}
}
}
}
You could use Vector3.Slerp(Vector3 StartPosition, Vector3 DestinationPosition, float Number)
Number is between 0 and 1 and it indicates where will be the position of your object between StartPosition and DestinationPosition.
Lets say Number = 0.0f;: your object will be at StartPosition.
If Number = 0.5f;: your object will be between StartPosition and DestinationPosition.
You need to increase the Number value from 0 to 1 when swipe action is performed.
The faster you increase the "Number" value, the faster your object will move towards Destination.
You should set you StartPosition once when the swipe action begins, not give your transform.position repeatedly in your Vector3.Slerp() function.
You can find an example here in Unity Docs.
Hope this helps! Cheers!
I have a camera script where if my character is rotating, the camera rotates too. However, the camera can also be rotated my the mouse pointer. This movement is restricted by a specific set of given degrees, say 30 (maximumX). So the range of camera movement controlled by the mouse should be between 0 and 60 then which is equivalent to -30 and 30 degrees.
If the character rotates a full 360 degrees, then the maxX value overlaps 360 and has to go back to 0. While min may remain something like 300. This creates an issue where the camera will "snap" and not preserve its previous rotation properly.
var newMaxX = ClampAngle(_target.localEulerAngles.y + maximumX, -360, 360);
var newMinX = ClampAngle(_target.localEulerAngles.y - maximumX, -360, 360);
if(newMinX > newMaxX)
{
var tmp = newMaxX;
newMaxX = newMinX;
newMinX = tmp;
}
if (axes == RotationAxes.MouseXAndY) {
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, newMinX, newMaxX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
cameraTransform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, newMinX, newMaxX);
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
cameraTransform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
cameraTransform.localRotation = originalRotation * yQuaternion;
}
Here's the ClampAngle function:
static function ClampAngle (angle : float, min : float, max : float) : float
{
if (angle < -360.0)
angle += 360.0;
if (angle > 360.0)
angle -= 360.0;
return Mathf.Clamp (angle, min, max);
}
Any recommendations?
The issue I discovered was with using:
_target.localEulerAngles.y
as a base. This is because localEulerAngels only go from 0 to 360 while running. In the editor, it isn't clamped. When running, it is clamped. This changes things dramatically because initially at rotation 0, if you move left, it loops to 360. I was treating it as if it would go to -1. Thus, thinking my math was wrong or I wasn't clamping correctly.
Clamping wouldn't do anything because it wouldn't notice anything about 0 becoming 360.
So, I decided to treat it as -180 to 180 instead.
var eulerTargetY = _target.localEulerAngles.y;
var newMaxX = 0;
var newMinX = 0;
if(eulerTargetY > 180)
{
var adjustedEuler = eulerTargetY - 360;
newMaxX = adjustedEuler + m_rangeX;
newMinX = adjustedEuler - m_rangeX;
}
else
{
newMaxX = eulerTargetY + m_rangeX;
newMinX = eulerTargetY - m_rangeX;
}
var xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
var yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotationX = ClampAngle (rotationX, newMinX, newMaxX);
if (axes == RotationAxes.MouseXAndY) {
cameraTransform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
cameraTransform.localRotation = originalRotation * xQuaternion;
}
else
{
cameraTransform.localRotation = originalRotation * yQuaternion;
}
For clarity, I made maximumX into m_rangeX, because previous I had it as 2 seperate units and I'm using 30 both ways currently. So now, the camera can properly sweep where the player points his mouse between _target.localEulerAngles.y - 30 and _target.localEulerAngles.y + 30 with circular overlap.
Ok im new to javascript and Unity so this is clearly a very noob beginner question.
Basically I have created a script that i place on my camera. Then when i select a model in the game it becomes the target of the camera script and im trying to make the camera smoothly turn to it. below is what im trying
in the update function i use raycast to catch a unit on the mouse click. this becomes the target variable in the script. it sets a bool to true then to follow target in the moveto function. but it only seems to move the camera if i keep the mouse left button pressed down on the unit. Im trying to select a unit then let the camera smoothly rotate to the unit.
Any ideas on this rookie mistake?
#pragma strict
var speed = 5.0;
var freeMovementTurnSpeed = 10.0;
var freeMovementForwardSpeed = 10.0;
var freeMovementVerticalSpeed = 10.0;
var maxHeight = 30;
var minHeight = 2;
var target : Transform;
var distance = 10.0;
var maxDistance = 50;
var minDistance = 10;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
// The distance in the x-z plane to the target
var followDistance = 30.0;
var maxDistanceDelta = 0.2;
var damping = 3.0;
var followTarget = false;
var smoothRotation ;
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if(target){
transform.LookAt(target);
}
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if(target){
if(followTarget){
moveTo();
}
else{
targetSelectedMovement();
}
}
else {
freeMovement();
}
}
function moveTo(){
//transform.LookAt (target);
var currentDistance = Vector3.Distance(transform.position,target.position);
//if (currentDistance<40){
//followTarget = false;
//}
//else{
smoothRotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, smoothRotation, Time.deltaTime * damping);
print(target);
//transform.position = Vector3.MoveTowards(transform.position,target.position,maxDistanceDelta);
//}
}
function freeMovement(){
var zPos = Input.GetAxis("Vertical") * Time.deltaTime * freeMovementForwardSpeed;
var xPos = Input.GetAxis("Horizontal") * Time.deltaTime * freeMovementTurnSpeed;
var strafePos = Input.GetAxis("Strafe")* Time.deltaTime * freeMovementForwardSpeed;
var vertPos = Input.GetAxis("GainVertical")* Time.deltaTime * freeMovementVerticalSpeed;
if (Input.GetButton("Fire2")) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation2 = Quaternion.Euler(y, x, 0);
transform.rotation = rotation2;
}
else {
transform.Rotate(0, Input.GetAxis("Horizontal")*freeMovementTurnSpeed*Time.deltaTime, 0,Space.World);
}
if (vertPos > 0 && transform.position.y > maxHeight){
vertPos = 0;
}
else if(vertPos < 0 && transform.position.y < minHeight){
vertPos = 0;
}
print(transform.position.y);
transform.Translate(strafePos,vertPos,zPos);
}
function targetSelectedMovement() {
if(Input.GetKey("a") || Input.GetKey("d") ){
x += (-Input.GetAxis("Horizontal")) * xSpeed * 0.02;
var rotation = Quaternion.Euler(y, x, 0);
var currentDistance = Vector3.Distance(transform.position,target.position);
var position = rotation * Vector3(0.0, 0.0, -currentDistance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
var zPos = Input.GetAxis("Vertical") * Time.deltaTime * speed;
currentDistance = Vector3.Distance(transform.position,target.position);
if(currentDistance < maxDistance && zPos < 0){
transform.Translate(0, 0, zPos);
}
else if(currentDistance > minDistance && zPos > 0){
transform.Translate(0, 0, zPos);
}
if (Input.GetButton("Fire2")) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation2 = Quaternion.Euler(y, x, 0);
var currentDistance2 = Vector3.Distance(transform.position,target.position);
var position2 = rotation2 * Vector3(0.0, 0.0, -currentDistance2) + target.position;
transform.rotation = rotation2;
transform.position = position2;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
function Update () {
var hit : RaycastHit;
if (Input.GetButton("Fire1")) {
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Cast a ray forward from our position for the specified distance
if (Physics.Raycast(ray, hit))
{
// Add a specified force to the Rigidbody component to the other GameObject
if(hit.collider.tag == "Unit"){
print("Selected " + hit.collider.gameObject.name);
target = hit.collider.gameObject.transform;
//transform.LookAt(target);
followTarget = true;
}
else {
target = null;
}
}
}
}
:)
Answer pulled from my comments above:
"I'll have to look when I get on my dev. machine but in the mean time try replacing Input.GetButton("Fire1") with Input.GetButtonDown("Fire1"). Just as a test, because looking at your code nothing jumps out at me. GetButton is true as long as the button is held, in the past I have had issues with it incorrectly triggering just as the mouse moved off an object."