- Home /
Jumping animation? Moving through objects? Syncing jump animation?
My player move back to the start position and slowly returns back to where he was as opposed to just moving up 1 slowly to simulate a jump. Also my jump animation and when he moves are not synced together how can i achieve this? Also I need to stop the player from going through objects like blocks and instead stop when he gets to them (Enless the player presses jump to jump onto it) Here is a video showing there problems. Link to Video
Here is my code(Im pretty sure the problem is in the last if statement):
#pragma strict
//Calling Variables
var hit : RaycastHit;
var ray : Ray;
var direction : Vector3;
var moveSpeed : float;
var rotateSpeed : float;
var PlayerTransform : Transform;
var targetPoint : Vector3;
var targetRotation : Quaternion;
var anim : Animator;
var speed : float;
var vara : float;
var distance : Vector3;
var isGrounded : boolean = false;
var Jump : boolean;
var positiona : float;
var positionb : float;
var positionc : float;
var positiond : Vector3;
function Start () {
//Sets the correct numbers for speeds
moveSpeed = 3;
rotateSpeed = 50;
PlayerTransform = transform;
direction = PlayerTransform.position;
anim = GetComponent(Animator);
Jump = false;
}
function Update () {
//Moves player twoords the raycast hitpoint
PlayerTransform.position = Vector3.MoveTowards(PlayerTransform.position, targetPoint, moveSpeed * Time.deltaTime);
//if the vector3 data of the raycast hitpoint and the players transform are = then stop the player from running or walking
if(targetPoint == PlayerTransform.position){
speed = 0;
moveSpeed = 3;
}
Jump = false;
anim.SetBool("Jump", Jump);
//Send the speed of the player to the animator so it can update the animation
anim.SetFloat("Speed", speed);
//If the player clicks the mouse then send a ray out
if(Input.GetMouseButton(0))
{
speed = 1;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, hit, 100))
{
direction = hit.point;
targetPoint = hit.point;
//Find the degree you need to rotate
targetRotation = Quaternion.LookRotation(targetPoint - PlayerTransform.position);
//Rotate the player to look at target
transform.rotation = Quaternion.Slerp(PlayerTransform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
targetPoint.y = 0;
//Send the speed of the player to the animator
anim.SetFloat("Speed", speed);
}
}
//If the shift key is being held down then make the player run
if(Input.GetKey(KeyCode.LeftShift) && targetPoint != PlayerTransform.position)
{
speed = 2;
moveSpeed = 5;
anim.SetFloat("Speed", speed);
}
//If the shift key is not being held down then tell the player to walk if he is not at his destination yet
else if(targetPoint != PlayerTransform.position){
speed = 1;
moveSpeed = 3;
anim.SetFloat("Speed", speed);
}
if (Input.GetKey(KeyCode.Space))
{
Jump = true;
anim.SetBool("Jump", Jump);
positiona = 1;
PlayerTransform.position.x = positionb;
PlayerTransform.position.z = positionc;
positiond = Vector3(positionb, positiona, positionc);
PlayerTransform.position = Vector3.Lerp(PlayerTransform.position, positiond, Time.deltaTime / 100);
}
}
Bumping post. I really need help with this I have been stuck thinking about this for days.
Answer by GiyomuGames · Nov 04, 2015 at 05:43 AM
You should maybe add a rigidbody to your player. Then for jump you will just have to add a vertical force when space is pressed. Your player probably has colliders on him in which case he really should have a rigidbody anyway. To move your player around you won't be able to change his transform directly (well, it's not recommended), but you can change it's velocity directly or use the MovePosition method of the rigidbody.
If you don't want to use a rigidbody you should launch a coroutine when space is pressed. The coroutine will update the z position over time, simulating gravity.
Also you need to be careful that the code after if (Input.GetKey(KeyCode.Space))
is only called once. Right now it can be called several times in a row depending on how often Update is called and how long you press Space.
For the player not to be able to go through blocks, you need to have non-trigger colliders on your player and on the blocks. Then make sure in your project settings > Physics that they are set to interact (this will depend on their respective layers).
Sorry it is probably a bit unclear for you if you are not too familiar with Unity, but hopefully that should help you go in the right direction.
Perfect thanks. I was using unity a couple months ago but got bored. I forgot alot. Thanks for the refresher!
Answer by ringthane · Nov 04, 2015 at 06:53 PM
Updated code for future reference #pragma strict //Calling Variables var hit : RaycastHit; var ray : Ray; var direction : Vector3; var moveSpeed : float; var rotateSpeed : float; var PlayerTransform : Transform; var targetPoint : Vector3; var targetRotation : Quaternion; var anim : Animator; var speed : float; var vara : float; var distance : Vector3; var isGrounded : boolean = false; var Jump : boolean; var positiona : float; var positionb : float; var positionc : float; var positiond : Vector3; var jumpPower : float; function Start () { //Sets the correct numbers for speeds moveSpeed = 3; rotateSpeed = 50; PlayerTransform = transform; direction = PlayerTransform.position; anim = GetComponent(Animator); Jump = false; jumpPower = 300; }
function Update () {
//Moves player twoords the raycast hitpoint
PlayerTransform.position = Vector3.MoveTowards(PlayerTransform.position, targetPoint, moveSpeed * Time.deltaTime);
//if the vector3 data of the raycast hitpoint and the players transform are = then stop the player from running or walking
if(targetPoint == PlayerTransform.position){
speed = 0;
moveSpeed = 3;
}
Jump = false;
anim.SetBool("Jump", Jump);
//Send the speed of the player to the animator so it can update the animation
anim.SetFloat("Speed", speed);
//If the player clicks the mouse then send a ray out
if(Input.GetMouseButton(0))
{
speed = 1;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, hit, 100))
{
direction = hit.point;
targetPoint = hit.point;
//Find the degree you need to rotate
targetRotation = Quaternion.LookRotation(targetPoint - PlayerTransform.position);
//Rotate the player to look at target
transform.rotation = Quaternion.Slerp(PlayerTransform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
targetPoint.y = 0;
//Send the speed of the player to the animator
anim.SetFloat("Speed", speed);
}
}
//If the shift key is being held down then make the player run
if(Input.GetKey(KeyCode.LeftShift) && targetPoint != PlayerTransform.position)
{
speed = 2;
moveSpeed = 5;
anim.SetFloat("Speed", speed);
}
//If the shift key is not being held down then tell the player to walk if he is not at his destination yet
else if(targetPoint != PlayerTransform.position){
speed = 1;
moveSpeed = 3;
anim.SetFloat("Speed", speed);
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump = true;
anim.SetBool("Jump", Jump);
GetComponent.<Rigidbody>().AddForce(transform.up*jumpPower);
//Old jumping code
//positiona = 1;
//PlayerTransform.position.x = positionb;
//PlayerTransform.position.z = positionc;
//positiond = Vector3(positionb, positiona, positionc);
//*PlayerTransform.position = Vector3.Lerp(PlayerTransform.position, positiond, Time.deltaTime / 100);
}
}
Your answer
Follow this Question
Related Questions
How to position the jump animation with the object's body? 1 Answer
Jumping Issue, Need Help. 0 Answers
Choose Start and End Frame for 2D Animation 0 Answers
How Can I get the mouse to Cycle Through Animations Each Time I click it? Please Help! 1 Answer
Best way to implement jump method for character controller 1 Answer