• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AnaRhisT · Jul 02, 2010 at 01:47 PM · positionmove

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 ;

CubeMovement

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); }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

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 &amp;&amp; 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 &lt; -360F)
         angle += 360F;
     if (angle &gt; 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;
        }
}
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Herman-Tulleken · Oct 26, 2010 at 04:52 AM 1
Share

Perhaps you could mark the answer so that the Community user won't bunmp the question?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Movement restriction... 1 Answer

Dynamically changing a decals position on a mesh surface 2 Answers

Why is my cursor not moving with the updating position of the raycast hit point? 0 Answers

How to have a projectile move towards player's last position and go passed that position in the same direction? 1 Answer

Problem with lerp positioning 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges