Help! Player script one step at a time add rotate and jump
hi ive set up a script to mimic pokemons tile by tile movement system this worked fine but i wanted something slightly different imagine temple run on an open plane, dependant on an invisible grid for movement. so the player should be constantly moving forward, rotation just rotates on the spot and the player carries on going straight
right now im depending on a start point and ending point and lerping from one to the other which isnt as smooth as it maybe could be but im still a noob at unity and scripting. my main issue is the way ive used my vector 3 its constantly adding to the x axis so when i rotate 90' it looks like im moving to the side because the script doesnt know ive rotated
anything in multi line comments doesnt work correctly but is what ive tried using, (it rotates the player but i dont know how to update the rest to say if (rotation == 90) { do this }
public int rotateSpeed;
public float speed;
public float increment;
public float distToGround;
public float jumpSpeed;
public Vector3 startPoint;
public Vector3 endPoint;
public bool isMoving;
public bool isGrounded;
public Rigidbody rb;
public CapsuleCollider sonicDroid;
void Start () {
startPoint = transform.position;
endPoint = transform.position;
rotateSpeed = 90;
jumpSpeed = 6.0f;
rb = sonicDroid.GetComponent<Rigidbody>();
rb.AddForce(1, 1, 1);
distToGround = sonicDroid.bounds.extents.y;
}
void Update () {
if(increment <=1 && isMoving == true)
{
increment += speed/10;
}
else
{
isMoving = false;
isGrounded = true;
}
if (isMoving)
transform.position = Vector3.Lerp(startPoint, endPoint, increment);
if (isMoving == false)
{
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
}
//turn left
/**
else if(Input.GetKey("a")&& isMoving == true){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z);
transform.Rotate(Vector3(0,90,0));
}
**/
//turn right
/**
else if(Input.GetKey("d")&& isMoving == true){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z);
transform.Rotate(Vector3(0,90,0));
}
**/
else if (Input.GetKeyDown("space")&& isGrounded == true)
{
rb.velocity += Vector3.up * jumpSpeed;
isMoving = false;
isGrounded = false;
}
}
}
it also wont jump? which is strange as i think my code i okay? and and all suggestions welcome
@martipello I'm just about to switch off for the night but if you haven't had any answers when I get back on tomorrow I'll have a look. This is more of a re$$anonymous$$der for me to do that than anything else. Please let me know if you get it sorted though so I won't waste my time.
@adelphiaU$$anonymous$$ I've also switched off for the night, its Sunday! But no joy thus far, please if your up for it have a crack and let me know, thanks
Answer by adelphiaUK · Dec 14, 2015 at 12:09 PM
OK @martipello, I've had a play but I'm not sure if this is 100% what you are after, but give it a go anyway. You will notice I have used different methods altogether and have also changed some variables from public to local. I created a new package from scratch so if you want to see what this does by itself, let me know and I'll simply add the package in.
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
public float rotateSpeed = 0.1f;
public float speed = 0.1f;
public float jumpSpeed = 6.0f;
public float maxDistance = 1.0f;
float distToGround;
float journeyDistance;
float startTime;
bool isGrounded = false;
bool doMovement = false;
Vector3 startPoint;
Vector3 endPoint;
Rigidbody rb;
Transform sonicDroid;
void Awake() {
sonicDroid = transform;
rb = GetComponent<Rigidbody>();
}
void Start() {
startPoint = sonicDroid.position;
endPoint = startPoint;
startTime = Time.time;
rb.AddForce(1, 1, 1);
journeyDistance = Vector3.Distance(startPoint, endPoint);
}
void Update() {
if(isGrounded) {
if(Input.GetKeyDown("space")) {
doMovement = false;
isGrounded = false;
rb.velocity = Vector3.up * jumpSpeed;
} else if(doMovement) {
if(Input.GetKey("a")) {
sonicDroid.Rotate(new Vector3(0, 90 * rotateSpeed, 0));
} else if(Input.GetKey("d")) {
sonicDroid.Rotate(new Vector3(0, -90 * rotateSpeed, 0));
}
float distanceMoved = (Time.time - startTime) * speed;
float journeyFraction = distanceMoved / journeyDistance;
sonicDroid.position = Vector3.Lerp(startPoint, endPoint, journeyFraction);
float distance = Vector3.Distance(sonicDroid.position, endPoint);
if(distance < maxDistance) {
Debug.Log("distance:" + distance);
startPoint = sonicDroid.position;
endPoint += (sonicDroid.forward * speed);
} else {
Debug.Log("distance:" + distance);
}
}
}
}
void OnCollisionEnter(Collision collision) {
if(!isGrounded && collision.collider.name.ToLower() == "floor") {
isGrounded = true;
doMovement = isGrounded;
if(isGrounded) {
startPoint = sonicDroid.position;
endPoint = startPoint + (sonicDroid.forward * speed);
}
}
}
}
okay this didn't work at all (probably my fault) did literally nothing and i think its due to a custom plane i am using (low polys), so i set up a new scene, a new capsule and a new plane to put the capsule on and added a rigidbody to the capsule, but the capsule just falls over, any ideas what im missing? Thank you very much for your time and effort though, hope we can get this working
You need to enable rotation constraints on the Rigidbody for X and Z. I used a custom plane too in my test project. The plane must be called "floor" in order for the collision detection to work or simply edit the code to check for whatever name you call the plane. You could alternatively, use a tag (ins$$anonymous$$d of name) for collision checking; I just prefer to use name as it's specific to a single object then, as long as your game object names are unique of course..
Are you on Google+ at all? I can always hook up with you live using hangouts if you are.
yes i am, and that would be awesome this is my profile https://plus.google.com/u/0/+martinseal/posts also i just tried again in my test scene but applied a collider to the plane and now everything runs almost perfectly hope to hear from you soon