Hello
I’ve just started a game development in unity, and now I have ran into a problem.
It’s regarding the movement of my player.
I have written a basic playerscript with “click to move” that uses rayhit to determine the position where the player is supposed to go and then rotates the player towards that position and simplemove forward…
I have tried to incorporate jumping to this with the help of charactermotor.
Everything works fine when I keep the mousebutton pressed down, because the position keeps getting updated.
If I jump without the mousebutton, my player jumps as supposed to, except for the landing which takes place behind the position of the previous mouseclick hence making the player run back after landing (or even midair before I figured out how to fix that).
I’m still new to unity and not the best programmer out there, so any help would be apperciated.
Here’s the script:
using UnityEngine;
using System.Collections;
public class PlayerScript_test : MonoBehaviour {
protected Animator PlayerAnimator;
private bool move = false;
public float speed; //Speed of the player
public CharacterController playercontroller; //PlayerController
private Vector3 position; //Position of the player
private CharacterMotor motor;
void Awake()
{
motor = GetComponent<CharacterMotor>();
PlayerAnimator = GetComponent<Animator>();
}
// Use this for initialization
void Start ()
{
position=transform.position; //Initialize player position
}
// Update is called once per frame
void Update ()
{
handleJump();
if(Input.GetMouseButton(0)) //If left mousebutton is clicked
{
if(playercontroller.isGrounded){
locatePosition(); //Locate where the player clicked..
}
}
else {PlayerAnimator.SetBool("Attack",false);}
if(move==true)
{
moveToPosition(); //Move to position (May need to change place once more logic is implemented)
}
}
void handleJump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if(!motor.inputJump){
motor.inputJump=true;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
motor.inputJump=false;
}
}
void locatePosition() //Locate position of left mousebutton click
{
RaycastHit hit;
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit,1000))
{
position = new Vector3(hit.point.x,hit.point.y,hit.point.z); //Return position of mouseclick
if(hit.collider.name.StartsWith("Enemy"))
{
move=false;
//rotate towards enemy then attack
lookAtPosition();
PlayerAnimator.SetFloat("Speed",0);
PlayerAnimator.SetBool("Attack",true);
}
else
{
PlayerAnimator.SetBool("Attack",false);
move=true;
}
}
}
void lookAtPosition()
{
Quaternion newRotation = Quaternion.LookRotation(position-transform.position);
newRotation.x=0f;
newRotation.z=0f;
transform.rotation=Quaternion.Slerp(transform.rotation,newRotation,Time.deltaTime*10);
}
void moveToPosition() //Move player object to position
{
float distance;
distance=Vector3.Distance(transform.position,position);
{
if(distance>1)
{
if(!motor.jumping.jumping)
{
lookAtPosition();
}
playercontroller.SimpleMove(transform.forward * speed);
PlayerAnimator.SetFloat("Speed",1);
}
else
{
PlayerAnimator.SetFloat("Speed",0);
}
}
}
}
Thank you in advance!
Best regards!