- Home /
Question by
danielmetlitski · Jan 04, 2015 at 04:58 PM ·
jumpingplatformplatform-support
GameObject starts drifting on X axis when doing high jump
Hey guys,my player game object keeps drifting on the X axis when he does a jump off platforms. This especially happens when he is high up, and during the jump I cant control where he lands! Below is my code. I appreciate any help, this issue has been giving me a headache for hours!
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
// Use this for initialization
Transform myTransform;
private float border = 6.5f;
private float speed = 5f;
void Start () {
myTransform = transform;
this.rigidbody.freezeRotation = true;
}
// Update is called once per frame
void Update () {
myTransform.position = new Vector3(myTransform.position.x, myTransform.position.y, 0f);
myTransform.rotation = Quaternion.identity;
if (Input.GetKey(KeyCode.D))
{
myTransform.Translate(Vector3.right * Time.deltaTime * speed);
}
else if (Input.GetKey(KeyCode.A))
{
myTransform.Translate(Vector3.left * Time.deltaTime * speed);
}
if (myTransform.position.x > border)
{
myTransform.position = new Vector3(-border, myTransform.position.y, myTransform.position.z);
}
else if (myTransform.position.x < -border)
{
myTransform.position = new Vector3(border, myTransform.position.y, myTransform.position.z);
}
}
void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Platform"))
{
if (Input.GetKeyDown(KeyCode.Space))
{
//myTransform.Translate(new Vector3(0, 4, 0));
this.gameObject.rigidbody2D.AddForce(new Vector3(0, 6, 0), ForceMode2D.Impulse);
}
}
}
}
Comment