- Home /
Move Object to Camera's position
Another question guys, So 2 things :
- The BLACK line represents the way I want it to move
- The YELLOW line represents the way it moves with the script below. Which is actually target.transform.position.x += 0.1 ; target.transform.position.x -= 0.1 ;
What I want to do it to move the cube to the Camera's X and Z axis. I thought about a method to distract the Camera's X an Z position from Cube's X and Z position and add to it +1 or something like that, I'd like to hear ur methods, I don't care to get a psuedo code, but most important to me that I'll understand how it HAS to work and implement that on my own. ThankIA.
var target : Transform; var distance = 10.0;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = -20; var yMaxLimit = 80;
private var x = 0.0; private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () { if (target && 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 rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
target.transform.position.x += 0.1 ;
}
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
target.transform.position.x -= 0.1 ;
}
}
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); }
Answer by AnaRhisT · Jul 02, 2010 at 08:46 PM
Solved.
Edited MouseLook.cs
using UnityEngine; using System.Collections;
/// MouseLook rotates the transform based on the mouse delta. /// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character: /// - Create a capsule. /// - Add a rigid body to the capsule /// - Add the MouseLook script to the capsule. /// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it) /// - Add FPSWalker script to the capsule
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform. /// - Add a MouseLook script to the camera. /// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.) [AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
public Transform Horse; //Yoor model
void Update ()
{
if (axes == RotationAxes.MouseXAndY && Input.GetButton("Fire2"))
{
// Read the mouse input axis
Horse.Rotate(0,Input.GetAxis("Mouse X") * sensitivityX,0); // Your model rotation
// rotationX += Input.GetAxis("Mouse X") * sensitivityX;
// rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
// rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
// Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
Horse.Rotate(0,Input.GetAxis("Mouse X") * sensitivityX,0);
//rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
Screen.lockCursor = true;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
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);
}
}
Zoom.js
function Update () {
if (Input.GetKey (KeyCode.Alpha2))
{
Camera.main.transform.position.z = Camera.main.transform.position.z + 0.1;
}
if (Input.GetKey (KeyCode.Alpha1))
{
Camera.main.transform.position.z = Camera.main.transform.position.z - 0.1;
}
}
Perhaps you could mark the answer so that the Community user won't bunmp the question?