Hello All. I am very new at Unity and C# and I think this is my first ever forum post. I have some beginner experience in Java but that’s about it.
So I am writing… well lets be honest, trying to write a follow script to control a game object(Sphere) which is connected and relative to another game object.
Inputs touch and mouse.
The Sphere should follow the mouse as I navigate though the screen up and down left and right however the movement is constrained using mathf.clamp.
When the mouse/ touch is in the center of the screen the Sphere moves on the z axis to -1 as to not visually collide with the relative game object.
Sphere following mouse/touch:
I have managed to get the Sphere to move on the Z axis from -1 to 0 so it moves in a gradual semicircle motion while being moved on the X Axis.
However there is no movement on the Y Axis and the values which I am getting on the debug log don’t really add up.
Can anyone shed some light please. I have attached the Script below. Thank you.
using UnityEngine;
using System.Collections;
public class Follow_Script : MonoBehaviour {
// Range of movement on X axis this will set from x to -x.
public float clampForX = 2;
//Value for minimum/lowest movement value on Y axis.
public float clampForYmin = -5;
//Value for maximum/highest movement value on Y axis.
public float clampForYmax = 10;
//Value for where the Game object starts off on the Z axis.
public float startPositionOnZAxis = 0;
void FixedUpdate ()
{
MoveUpdate ();
}
void MoveUpdate () {
//Vector3 to store position of mouse or touch
Vector3 pos;
//keepUpWithPointX to store float value of X axis from pos for mathf.clamp
float keepUpWithPointX;
//keepUpWithPointY to store float value of Y axis from pos for mathf.clamp
float keepUpWithPointY;
// Variable used to hold mathf.clamp value of X axis
float clampX;
// Variable used to hold mathf.clamp value of Y axis
float clampY;
//if Android is running get touch input and store into Vector3 pos
if (Application.platform == RuntimePlatform.Android)
{
pos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.GetTouch (0).position.x,
Input.GetTouch (0).position.y, -1));
}
// else to add in mouse input for testing mostly
else
{
pos = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x,
Input.mousePosition.y, -1));
}
// Passing X Axis position of Game object value to keepUpWithPointX
// Cursor was not following the mose properly and was lagging behind.
// * by 30f seems to have solved it with the mouse as input
// although I have not tried on a touch device yet.
keepUpWithPointX = pos.x * 30f;
// Passing Y Axis position of Game object to keepUpWithPointY
// This is giving a strange value and not working as expected.
// Multiplying by 30f does not help.
keepUpWithPointY = pos.y;
// Added Debug to check what is happening when passing over values.
Debug.Log ("pos.y " + pos.y + " " + "keepUpWithPointY " + keepUpWithPointY);
// Clamping X Axis Values into clampX
clampX = Mathf.Clamp (keepUpWithPointX, -clampForX, clampForX);
// Clamping Y Axis Values into clampY
clampY = Mathf.Clamp (keepUpWithPointY, clampForYmin, clampForYmax);
Debug.Log ("clampY " + clampY);
/* Give object new position by passing X and Y as Clamp values and calling Rotation
* in order to move the game object in fixed space on Z axis.
*/
transform.position = new Vector3 ( -clampX, -clampY, Rotation (clampX));
}
/* create motion for Static Game Object.
* When mouse/touch at center of sceen the game object moves out on the z axis to 1-
* When mouse/touch is at max or min of mathf.clamp for movement them game object is
* moved gradually to 0.
* Basically Range 1 from 3 to 0
* Range 2 from 0 to -1
* When Range 1 = 3 , Range 2 = 0
*/
float Rotation(float clamp){
// change from 0 to 1;
float change;
float rotation;
float absVal = Mathf.Abs (clamp);
change = absVal / clampForX;
rotation = change - 1;
return rotation;
}
}