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 voodoo664 · May 28, 2015 at 04:14 AM · transformpositionvector3intlock

transform.position as variable to lock object

is it possible to use vector3 as a variable and be able to lock the y axis of a gameobject so that it always stays at that certain position, i have a script below but I am stuck using transform.position.z = -3.73; under my update function because i get the error vector3 cannot be converted from a int, and same with a float. I want it to lock on my y axis since i am working on a side scroller game but sometimes objects with knock my guy off or out of the map on my y axis.

 private var motor : CharacterMotor;
 
 
 // Use this for initialization
 function Awake () {
     motor = GetComponent(CharacterMotor);
 var directionVector = new Vector3(Input.GetAxis("Horizontal"),0);
     
     }
 
 // Update is called once per frame
 function Update () {
     // Get the input vector from keyboard or analog stick
 var directionVector = new Vector3(Input.GetAxis("Horizontal"),0);
 transform.eulerAngles.y = 0;
 transform.position.z = -3.73;
 
 
  if (directionVector != Vector3.zero) {
         // Get the length of the directon vector and then normalize it
         // Dividing by the length is cheaper than normalizing when we already have the length anyway
         var directionLength = directionVector.magnitude;
         directionVector = directionVector / directionLength;
         
         // Make sure the length is no bigger than 1
         directionLength = Mathf.Min(1, directionLength);
         
         // Make the input vector more sensitive towards the extremes and less sensitive in the middle
         // This makes it easier to control slow speeds when using analog sticks
         directionLength = directionLength * directionLength;
         
         // Multiply the normalized direction vector by the modified length
         directionVector = directionVector * directionLength;
 
  
     // Apply the direction to the CharacterMotor
     motor.inputMoveDirection = transform.rotation * directionVector;
     motor.inputJump = Input.GetButton("Jump");
  }
 }
 // Require a character controller to be attached to the same game object
 @script RequireComponent (CharacterMotor)
 @script AddComponentMenu ("Character/FPS Input Controller")
 
Comment
Add comment · Show 6
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 voodoo664 · May 28, 2015 at 04:28 AM 0
Share

$$anonymous$$issingFieldException: UnityEngine.Vector3.transform

I get this error trying to set the position using my players transform, it still wont lock but it looks fine in the script to my knowledge but I am still learning so anything could be a bit off here is the new line of code added.

 private var motor : Character$$anonymous$$otor;
  var playerPos : Vector3;
  var player : Transform;
 
 // Use this for initialization
 function Awake () {
     motor = GetComponent(Character$$anonymous$$otor);
 var directionVector = new Vector3(Input.GetAxis("Horizontal"),0);
     
     }
 
 // Update is called once per frame
 function Update () {
     // Get the input vector from keyboard or analog stick
 var directionVector = new Vector3(Input.GetAxis("Horizontal"),0);
 transform.eulerAngles.y = 0;
 player.transform.position = playerPos.transform.position;
avatar image maccabbe · May 28, 2015 at 06:03 AM 2
Share

It is much easier to find what is wrong with the entire error message since the error message tells you exactly what line the error occurs on. However you cannot use

 playerPos.transform.position

because playerPos is a Vector3 which has no transform component. Since it is a Vector3 you can just assign it to transform.position, i.e.

 player.transform.position = playerPos;


avatar image voodoo664 · May 28, 2015 at 06:24 AM 0
Share

That doesn't work, it just locks all of my positions and if i only set the z position it sets my guy at X position 0, and Y position 0 and the z position correctly so i then copied all of my positions and it just locked him. if i set it under the awake function it works on start but doesnt update his z position so he will still fall out of the map if he snags or gets stuck on an object.

avatar image siaran · May 28, 2015 at 10:39 AM 1
Share

If you want to lock your object's y position to, say, 0, what's wrong with saying transform.position.y = 0?

Actually that's not allowed in c# (i'm not sure about javascript), so you would have to do transform.position = new Vector3(transform.position.x, 0, transform.position.z);

Either way, if you just put something like that at the end of your Update() method your y-position will be locked?

avatar image voodoo664 · May 28, 2015 at 09:52 PM 0
Share

Yes that does work but I want to be able to just change the Z position so that it is locked under a variable so that i can easily change it since all of my levels are not positioned at 0. if i have a map thats positioned -3.5 on the z and my character is at 0 if it locks he will just fall because he isnt locked at -3.5, then the next level is at -6.7 but my character is still locked at 0, I have been trying to find a way so that the character can update his z position and lock it under a variable. the script i have works exactly how it should but if i have to duplicate a script multiple times for each level just to change my characters z position is not the healthiest way to script.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

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

Related Questions

transform.position = new Vector 3 NOT moving to correct position? 1 Answer

Setting target position in Vector3.MoveTowards 2 Answers

Creating a bouncing game without using physics - Vector3 math problem, 2 Answers

Trouble converting transform.position to C# 1 Answer

Trouble setting up a vector 3. 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