Player Floating
Hi i have this script, which make my player floats, when moved. I can't find out why, even when the plane is put to ground zero the the player always goes 1.08 y above the set y value, and I cant figure out why. Here is my code,
using UnityEngine; using System.Collections;
public class newone : MonoBehaviour { public float Speed = 10.0F; public float RotationSpeed = 100.0F; public static Animator Anim; public Vector3 MoveDirection; public double XPos = 10.67f; public double YPos = -11.68236f; public double ZPos = -1.49f;
CharacterController CharacterCont;
void Start()
{
CharacterCont = GetComponent<CharacterController>();
Anim = GetComponent<Animator>();
float NewX = (float)XPos;
float NewY = (float)YPos;
float NewZ = (float)ZPos;
transform.position = new Vector3 (NewX, NewY, NewZ);
}
void Update()
{
float NewY = (float)YPos;
Vector3 PositionY = transform.position;
PositionY.y = NewY;
float Translation = Input.GetAxis("Vertical") * Speed;
float Rotation = Input.GetAxis("Horizontal") * RotationSpeed;
Translation *= Time.deltaTime;
Rotation *= Time.deltaTime;
transform.Translate(0, 0, Translation);
transform.Rotate(0, Rotation, 0);
//transform.position = PositionY;
Vector3 MoveDirection = new Vector3(0, 0, Translation);
MoveDirection = transform.TransformDirection(MoveDirection);
if (Input.GetButton("Jump"))
{
Anim.SetTrigger("Jumping");
}
if (Translation == 0)
{
Anim.SetBool("isRunning", false);
Anim.SetBool("isIdle", true);
}
else
{
Anim.SetBool("isRunning", true);
Anim.SetBool("isIdle", false);
}
CharacterCont.Move(MoveDirection * Time.deltaTime);
}
}
I know this is quick and dirty but I'm pressed for time.... Divide your new axes position by ×0.5f.that is New Vector3 (X × 0.5f, Y ×0.5f, Z×0.5f); // sorry my mobile doesn't not have star for multiply It also may be that your patented to an object that is positioned such that 0 for your newly positioned object is correct and it is your parent object causing the change.
As another quick try out, just cache back the original position of the game object you want to change back to that new vector you made, just the Y part :) Sorry it's short and we'll well dirty but like said a bit pressed for time :(
In fairness you are making your Y axis go up by 11units or so. Was there a reason for that? Cheers bud