Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ringthane · Nov 03, 2015 at 09:02 PM · animationunity 5jumpjumpinganimations

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); 
 }
 }


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ringthane · Nov 03, 2015 at 09:54 PM 0
Share

Bumping post. I really need help with this I have been stuck thinking about this for days.

avatar image ringthane · Nov 04, 2015 at 12:04 AM 0
Share

Bumping post

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ringthane · Nov 04, 2015 at 06:24 PM 0
Share

Perfect thanks. I was using unity a couple months ago but got bored. I forgot alot. Thanks for the refresher!

avatar image
0

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); 
 }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges