Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Charsmud · Jul 29, 2012 at 02:29 AM · movementaxisvertical

Restricting movement on the vertical axis

I am using the following orbiting script:

     var otherObject : GameObject;
 
 var distance;
 function Start () {
 
 
 }
 
 function Update () {
  distance = Vector3.Distance(this.transform.position, otherObject.transform.position);
 
  if(distance !=50) {
  Debug.Log(distance);
    distance = 50;
    transform.position = (transform.position - otherObject.transform.position).normalized * distance + otherObject.transform.position;
 
  }
  var somePos : Vector3 = otherObject.transform.position;
     transform.RotateAround (somePos, transform.up, 20 * Time.deltaTime);
 }

This is doing what I want it to do, except for that when I decrease the distance between myself and the object this script is attached to, it moves up into the air instead of away from me. How do I fix this?

Comment
Add comment · Show 1
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 Charsmud · Jul 29, 2012 at 02:09 PM 0
Share

Anyone have any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ScroodgeM · Jul 29, 2012 at 07:59 PM

this script will move object like truck's trail when truck goes backward. so, if your other object's height is lower then this object, then this object will go upwards when you move towards it.

solution 1: clamp Y component of all calculations and height will no gives any affect

solution 2: as i understand you just rotating this transform around 'otherObject' with a constant distance 50. i'm sure the best way to achieve this is to attach this object to 'otherObject' as a child and just rotate it around 'otherObject'. distance will kept without any manual actions and relative height will also be constant.

solution 3:

using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour
{
    public Transform rotateAroundObject;
    public float rotationSpeed = 20f;
    public float keepDistance = 50f;
    float rotationAngle = 0f;
    void Update()
    {
        rotationAngle = Mathf.Repeat(rotationAngle + Time.deltaTime * rotationSpeed, 360f);
        Quaternion rotationNeed = Quaternion.AngleAxis(rotationAngle, Vector3.up);
        transform.position = rotateAroundObject.position + rotationNeed * Vector3.forward * keepDistance;
    }
}
Comment
Add comment · Show 7 · 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 Charsmud · Jul 29, 2012 at 08:03 PM 0
Share

the problem with solution 2 is that the object being orbited is the player, and when the player turns, the object turns with them. How would i clamp the Y component?

avatar image ScroodgeM · Jul 29, 2012 at 08:15 PM 0
Share

check solution 3

avatar image Charsmud · Jul 29, 2012 at 08:33 PM 0
Share

is this javascript? Sorry if i was not clear, I dont really know many other languages besides java and javascript (and i dont know javascript that well either)

avatar image ScroodgeM · Jul 30, 2012 at 04:22 AM 0
Share

no, this is C#, just save these lines to 'Rotator.cs' in your project folder

avatar image Charsmud · Jul 30, 2012 at 04:26 AM 0
Share

And then attach the script to the same object that is rotating around the player?

Show more comments

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I drag an object without Y transformation? 2 Answers

Vertical axis not working properly 0 Answers

Clamping movement range on X-axis 0 Answers

Why does the player keep pointing in a specific direction after moving? 0 Answers

Constrain axis of isometric camera ("vertical" only) 0 Answers


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