3 lane change scripting with vertical movement problem
I have a script I am working on and I believe I have the premise down but I am getting confused as to how to keep my 3 lane switching code and add in the Input.getaxis ("Vertical") to work so the character can move forward, or should I just write a script sepearate making my player always move forward (this is for an infinite runner style game)
the code is as follows: using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
float input = Input.GetAxis("Horizontal");
if(Mathf.Abs(Input) > deadZone) {
if(!didChangeLastFrame) {
didChangeLastFrame = true; //Prevent overshooting lanes
laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
if(laneNumber < 0) laneNumber = 0;
else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
} else {
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
}
take this code from update: CharacterController controller = GetComponent<CharacterController>();
and put it in a start function ins$$anonymous$$d, like this:
void Start(){
CharacterController controller = GetComponent<CharacterController>();
}
I moved the code as you suggested into a a start function, but I am now getting this error
error CS1525: Unexpected symbol `}' and also a parsing error, but my code is all closed in ??, or have I missed one somewhere
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Start()
{
CharacterController controller = GetComponent<CharacterController>();
}
void Update() {
float input = Input.GetAxis("Horizontal");
if($$anonymous$$athf.Abs(Input) > deadZone) {
if(!didChangeLastFrame) {
didChangeLastFrame = true; //Prevent overshooting lanes
laneNumber += $$anonymous$$athf.RoundToInt($$anonymous$$athf.Sign(input));
if(laneNumber < 0) laneNumber = 0;
else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
}
else
{
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove (moveDirection * Time.deltaTime)
}
}
ok I can get the player to move now but not in the 3 lane Horizontal anymore, he can move back and forwards now so that's a good start. when I hit the jump key he hangs in the air and slowly comes down, if I hit the jump key again he goes even higher, when I press the left/D key the character lowers back to the ground. code I have so far is as follows. I am out for the night its past midnight inthe land down under thanks for helping me out so far, will check in the morning, Thanks again. using UnityEngine; using System.Collections;
public class ThreeLaneRunner : $$anonymous$$onoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Start()
{
}
void Update() {
CharacterController controller = GetComponent<CharacterController>();
float input = Input.GetAxis("Horizontal");
if($$anonymous$$athf.Abs(input) > deadZone) {
if(!didChangeLastFrame) {
didChangeLastFrame = true; //Prevent overshooting lanes
laneNumber += $$anonymous$$athf.RoundToInt($$anonymous$$athf.Sign(input));
if(laneNumber < 0) laneNumber = 0;
else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
}
else
{
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove (moveDirection * Time.deltaTime);
}
}
ok so I noticed the Upper case on the input and changed t the same time you did, I have tried the code in the void Start and update and get the same problem both times, I can move forward and backwards, space for jump (endless Y movement) if I keep the button depressed. The right key "d" now works as a -y movement ins$$anonymous$$d of right and I have no left movement at all.
Answer by Dark Age · Jan 05, 2016 at 01:17 AM
Ok I have been able to work out the code for myself to get a 90% desired result, I can now change in only 3 lanes, jump, and I have also attached a script for zmovement. The last 10% is stopping the backwards movement. So for those wishing to do the same style of game/scripting I share with you what I have been able to achieve.
using UnityEngine;
using System.Collections;
public class PlayerControllerScript: MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public int laneNumber = 0;
public int lanesCount = 4;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = 0;
public float deadZone = 0.1f;
public float sideSpeed = 5;
void Update() {
//Access the objects Character Controller
CharacterController controller = GetComponent<CharacterController>();
float input = Input.GetAxis("Horizontal");
//Make sure the object is grounded before doing the math for lane changes
if (controller.isGrounded)
if(Mathf.Abs(input) > deadZone) {
if(!didChangeLastFrame) {
didChangeLastFrame = true; //Prevent overshooting lanes
laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
if(laneNumber < 0) laneNumber = 0;
else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
}
}
else
{
didChangeLastFrame = false;
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
Vector3 pos = transform.position;
pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
transform.position = pos;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
}
Go into unity and open Edit->project settings->Input
Find the entry that says "Vertical", open it up, find where it says "s" or "downarrow" or whatever the button you're using for backwards is, and delete that "s".
Awesome thanks $$anonymous$$big kudos to you
Your answer
Follow this Question
Related Questions
Need help about getAxis 0 Answers
Hi, got a problem with my vertical axis. 0 Answers
Using GetAxis with UI Buttons 0 Answers
Input Keyboard GetAxis not working 0 Answers