- Home /
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")
$$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;
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;
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.
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?
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.
Your answer
Follow this Question
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