How do i make character JUMP?
Hey guys I've got a request to ask could you help me implement jump into my script ? I tried so many variations and none of them worked. It is in C# and should be written on the line 65 // Y - Up and Down. Thanks for any advice !
using UnityEngine; using System.Collections;
public class PlayerMotor : MonoBehaviour { private CharacterController controller; private Vector3 moveVector;
private Vector3 moveDir = Vector3.zero;
private float speed = 5.0f;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float animationDuration = 3.0f;
private float startTime;
private bool isDead = false;
//Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
startTime = Time.time;
}
//Update is called once per frame
void Update()
{
if (isDead)
return;
if(Time.time - startTime < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
// X - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
if(Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.x > Screen.width / 2)
moveVector.x = speed;
else
moveVector.x = -speed;
}
// Y - Up and Down
moveVector.y = verticalVelocity;
// Z - Forward and Backward
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
public void SetSpeed(float modifier)
{
speed = 5.0f + modifier;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
Death();
}
private void Death()
{
isDead = true;
GetComponent<Score>().OnDeath();
}
}
Do you maybe want to do moveVector.y += verticalVelocity?
It hasnt changed anything, do i need to make a make an extra imput or something else because it doesnt react to any buttons. :P
hmm... i don't think you have an input for jumping yet. you would do something like your move function, like
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.SPACE)){
verticalVelocity=10.0;
}
You may want to switch to using a rigidbody2D ins$$anonymous$$d, they handle stuff like this well.
Answer by Pengocat · Feb 14, 2017 at 05:46 AM
The following script is based on your script and the official documentation for CharacterController.Move
It handles jumping. Perhaps it can help you.
using UnityEngine;
[RequireComponent(typeof(Score))]
public class PlayerMotor : MonoBehaviour
{
public float speed = 5.0f;
public float jumpSpeed = 8.0f;
public float gravity = 12.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
#region Unity Magic Methods
// Awake is called when the script instance is being loaded
void Awake()
{
controller = GetComponent<CharacterController>();
}
// Update is called every frame, if the MonoBehaviour is enabled
void Update()
{
UpdateControllerMovement();
}
// OnControllerColliderHit is called when the controller hits a collider while performing a Move
void OnControllerColliderHit(ControllerColliderHit hit)
{
bool isImpactPointMoreThanThis = hit.point.z > transform.position.z + 0.1f;
if (isImpactPointMoreThanThis && hit.gameObject.CompareTag("Enemy"))
{
Destroy(this);
}
}
// This function is called when the MonoBehaviour will be destroyed
void OnDestroy()
{
GetComponent<Score>().OnDeath();
}
#endregion
/// <summary>
/// Move the CharacterController according to player Input and gravity.
/// Note that Input is only evaluated when the controller is grounded.
/// </summary>
public void UpdateControllerMovement()
{
EvaluateMoveDirection();
controller.Move(moveDirection * Time.deltaTime);
}
private void EvaluateMoveDirection()
{
if (controller.isGrounded)
{
SetMoveDirectionByInput();
}
SetMoveDirectionByGravity();
}
private void SetMoveDirectionByInput()
{
moveDirection = new Vector3(
Input.GetAxis("Horizontal"),
0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
private void SetMoveDirectionByGravity()
{
moveDirection.y -= gravity * Time.deltaTime;
}
}
<3 Thank you so much it was driving me mad :D, it jumps the only bummer is that it slows down but thank you !
If you by slowing down are referring to on the way down then you can increase the gravity to make it drop faster.
Once again thank you and I am sorry that I am bothering you but am I supposed to run both scripts at once ? If so could you help me figure out how to make it so that player cant jump at the begining, as it is in the original script. Line 25 and 30.
Pengocat's script would replace the existing one.
void Update(){
tickCount++;
if(tickCount>60)
UpdateController$$anonymous$$ovement();
}
Thank you also! Your script was reporting that tickCount was not existing in current context so I used part from pengocat's script and than I spotted That I needed to make some private / public void for tickCount to make it work, so I sticked to his version as it was working . So thank you once again for you help.
You could just wrap the piece of code into an if with the startdelay counting down each frame.
using UnityEngine;
[RequireComponent(typeof(Score))]
public class Player$$anonymous$$otor : $$anonymous$$onoBehaviour
{
public float speed = 5.0f;
public float jumpSpeed = 8.0f;
public float gravity = 12.0f;
public float startDelay = 3f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
#region Unity $$anonymous$$agic $$anonymous$$ethods
// Awake is called when the script instance is being loaded
void Awake()
{
controller = GetComponent<CharacterController>();
}
// Update is called every frame, if the $$anonymous$$onoBehaviour is enabled
void Update()
{
if (startDelay > 0f)
{
$$anonymous$$oveForward();
startDelay -= Time.deltaTime;
}
else
{
UpdateController$$anonymous$$ovement();
}
}
// OnControllerColliderHit is called when the controller hits a collider while perfor$$anonymous$$g a $$anonymous$$ove
void OnControllerColliderHit(ControllerColliderHit hit)
{
bool isImpactPoint$$anonymous$$oreThanThis = hit.point.z > transform.position.z + 0.1f;
if (isImpactPoint$$anonymous$$oreThanThis && hit.gameObject.CompareTag("Enemy"))
{
Destroy(this);
}
}
// This function is called when the $$anonymous$$onoBehaviour will be destroyed
void OnDestroy()
{
GetComponent<Score>().OnDeath();
}
#endregion
/// <summary>
/// $$anonymous$$ove the CharacterController according to player Input and gravity.
/// Note that Input is only evaluated when the controller is grounded.
/// </summary>
public void UpdateController$$anonymous$$ovement()
{
Evaluate$$anonymous$$oveDirection();
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
private void Evaluate$$anonymous$$oveDirection()
{
if (controller.isGrounded)
{
Set$$anonymous$$oveDirectionByInput();
}
Set$$anonymous$$oveDirectionByGravity();
}
private void $$anonymous$$oveForward()
{
controller.$$anonymous$$ove(Vector3.forward * speed * Time.deltaTime);
}
private void Set$$anonymous$$oveDirectionByInput()
{
moveDirection = new Vector3(
Input.GetAxis("Horizontal"),
0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
private void Set$$anonymous$$oveDirectionByGravity()
{
moveDirection.y -= gravity * Time.deltaTime;
}
}
Thanks ! It works now, i am using my main script and the second one with added delay from the third one, the problem i had with the slowing down while running was caused by 2 speeds on player one from my main script and one from the second script so i have put it to 0 and it works fine now. Once again thanks for help !
Your answer
Follow this Question
Related Questions
i need help jump script c# 1 Answer
Unity GetComponent Script? 1 Answer
Saving Data (please help) 0 Answers
Audio on keydown Won't stop 1 Answer
I create material with script but it does not render right 0 Answers