Hi all, I’m new to unity and kind of new to a degree to programing, I’ve been looking at tutorials and guides to get the movement in my game working somewhat but until recently I’ve encountered an issue where the player will only walk about two steps then will stop, then he will become erratic until I let go and press again.
Could anyone help me with this as the movement is far from intended, also are there anyways of adding force to a ball in the direction the player is moving but preventing the player from being affected by the collision.
My code is below.
import UnityEngine;
import System.Collections;
import UnityEngine.Animator;
var LeftButton : String;
var RightButton : String;
var ForwardButton : String;
var BackButton : String;
var RunButton : String;
var AttackButton : String;
var Dance : String;
var Jump : String;
var Forward : Vector3;
var Reverse : Vector3;
var DT : double;
var LeftRotation : float;
var LeftForwardRotation : float;
var ForwardRotation : float;
var ForwardRightRotation : float;
var RightRotation : float;
var RightBackRotation : float;
var BackRotation : float;
var LeftBackRotation : float;
var JumpHeight : float;
var Suspended : boolean;
var MoveSpeed : double;
var isWalking : boolean;
var isFalling : boolean;
var isJumping : boolean;
var isDancing : boolean;
var isStanding : boolean;
var directionsPressed : boolean;
var anim : Animator;
var cam : Camera;
function Start () {
anim = GetComponent("Animator");
ForwardButton = "w";
LeftButton = "a";
RightButton = "d";
BackButton = "s";
RunButton = "u";
Dance = "k";
AttackButton = "j";
Forward = new Vector3 (0, 0, -2);
Reverse = new Vector3 (0, 0, 2);
JumpHeight = 200;
DT = Time.deltaTime;
//45 Degree Angles
ForwardRotation = 0;
ForwardRightRotation = 45;
RightRotation = 90;
RightBackRotation = 135;
BackRotation = 180;
LeftBackRotation = 225;
LeftRotation = 270;
LeftForwardRotation = 315;
Suspended = false;
MoveSpeed = 20.0;
}
function Update () {
if (Suspended)
{
return;
}
//-----------------------------------------------------------------
if (Input.GetButton(RunButton))
{
MoveSpeed = 4;
}
else
{
MoveSpeed = 2;
}
if (directionsPressed == true) {
if (Input.GetButton(ForwardButton))
{
transform.rotation.eulerAngles.y = ForwardRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT);
isWalking = true;
}
if (Input.GetButton(BackButton))
{
transform.rotation.eulerAngles.y = BackRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT);
isWalking = true;
}
if (Input.GetButton(LeftButton))
{
transform.rotation.eulerAngles.y = LeftRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT);
isWalking = true;
}
if (Input.GetButton(RightButton))
{
transform.rotation.eulerAngles.y = RightRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT);
isWalking = true;
}
if (Input.GetButton(LeftButton) && (Input.GetButton(ForwardButton)))
{
transform.rotation.eulerAngles.y = LeftForwardRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT / 4);
isWalking = true;
}
if (Input.GetButton(RightButton) && (Input.GetButton(ForwardButton)))
{
transform.rotation.eulerAngles.y = ForwardRightRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT / 4);
isWalking = true;
}
if (Input.GetButton(RightButton) && (Input.GetButton(BackButton)))
{
transform.rotation.eulerAngles.y = RightBackRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward *MoveSpeed * DT / 4);
isWalking = true;
}
if (Input.GetButton(LeftButton) && (Input.GetButton(BackButton)))
{
transform.rotation.eulerAngles.y = LeftBackRotation - Camera.current.transform.rotation.eulerAngles.y;
transform.Translate(Forward * MoveSpeed * DT / 4);
isWalking = true;
}
}
//Check if Directions are being pressed.
if (Input.GetButton(LeftButton))
{
//Debug.Log("Left Pressed");
directionsPressed = true;
}
if (Input.GetButton(RightButton))
{
//Debug.Log("Right Pressed");
directionsPressed = true;
}
if (Input.GetButton(ForwardButton))
{
directionsPressed = true;
}
if (Input.GetButton(BackButton))
{
directionsPressed = true;
}
//Check if Directions are being let go.
if (Input.GetButtonUp(LeftButton))
{
directionsPressed = false;
isWalking = false;
}
if (Input.GetButtonUp(RightButton))
{
directionsPressed = false;
isWalking = false;
}
if (Input.GetButtonUp(ForwardButton))
{
directionsPressed = false;
isWalking = false;
}
if (Input.GetButtonUp(BackButton))
{
directionsPressed = false;
isWalking = false;
}
//----------------------------------------------------------------
if (directionsPressed == false && isJumping == false)
{
//Debug.Log("I'm now standing.");
//animation.Play("Standing_Still");
anim.SetInteger("AnimationtoPlay", 0);
isStanding = true;
}
/*
if (isJumping == false && isWalking == true)
{
Debug.Log("I should be walking now.");
anim.SetInteger("AnimationtoPlay", 2);
animation.Stop("FALLING");
isStanding = false;
isJumping = false;
isDancing = false;
isWalking = true;
}
*/
/*
if (Input.GetButton(Dance))
{
Debug.Log("Look at me dance!");
anim.SetInteger("AnimationtoPlay", 3);
//animation.Stop("Walking");
//animation.Stop("Jumping");
//animation.Stop("Standing_Still");
//animation.Play("Dance");
isStanding = false;
isWalking = false;
isJumping = false;
isDancing = true;
}
*/
if (isJumping == false && (Input.GetButtonDown("Jump")))
{
Debug.Log("I should be jumping up now.");
rigidbody.AddForce(Vector3.up *JumpHeight);
animation.Stop("Walking");
anim.SetInteger("AnimationtoPlay", 1);
isStanding = false;
isDancing = false;
isWalking = false;
isJumping = true;
}
if (Input.GetButton("AttackButton"))
{
anim.SetInteger("AnimationtoPlay", 4);
}
}
function OnCollisionEnter(theCollision : Collision)
{
if (theCollision.gameObject.name == "Field")
{
animation.Stop("Jumping");
animation.Stop("FALLING");
isJumping = false;
isFalling = false;
isStanding = true;
isDancing = false;
Debug.Log("I landed on the floor");
}
/*
else if (theCollision.gameObject.name == "Single Block")
{
Debug.Log("Hit the wall");
}
*/
}
function SetSuspension(setting : boolean)
{
Suspended = setting;
}