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 bobonik · Feb 01, 2018 at 11:17 AM · velocitybouncemoveposition

movePosition affects Velocity

Hello, I'm making a platformer, where you are jumping every time you touch a platform and you can drag your character with touch position. The problem is I'm using rigidbody's movePosition method to drag the player, but this affects the velocity that I'm setting, so my character could Jump. Every time I try to drag my player - the velocity.y freezes it's value as the last one before starting to drag. Any solutions?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnitedCoders · Feb 01, 2018 at 12:58 PM

Share your code sample you are using to perform these functionality. to rectify your problem.

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
avatar image
0

Answer by bobonik · Feb 01, 2018 at 01:46 PM

@UnitedCoders

Here is my code, attached to the player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
 
     public float bounceForce;
     private GameObject player;
     private Rigidbody2D rb;
 
     private Vector3 pos;
     private Vector3 v3P1;
 
     private bool movePlayer1;
     private bool draggingP1;
 
     private Ray ray;
     private RaycastHit2D hit;
     private Transform toDrag;
 
     void Start()
     {        
         player = this.gameObject;
         rb = player.GetComponent<Rigidbody2D>();
 
         draggingP1 = false;
         
         toDrag = gameObject.transform;
     }
 
     private void Update()
     {
         pos = Input.mousePosition;
         ray = Camera.main.ScreenPointToRay(pos);
 
         if (Input.GetMouseButtonDown(0))
         {
             hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
 
             if (hit && (hit.collider.tag == "Player1"))
             {
                 //toDrag = hit.transform;
                 //dist = hit.transform.position.z - Camera.main.transform.position.z;
                 v3P1 = new Vector3(ray.origin.x, toDrag.position.y, 0);
                 v3P1 = Camera.main.ScreenToWorldPoint(v3P1);
                 //offset = toDrag.position - v3;
                 draggingP1 = true;
             }
         }
         if (Input.GetMouseButton(0) && draggingP1)
         {
             movePlayer1 = true;
             v3P1 = new Vector3(ray.origin.x, toDrag.position.y, 0);
             //rb.MovePosition(v3);
         }
         if (Input.GetMouseButtonUp(0) && draggingP1)
         {
             movePlayer1 = false;
             draggingP1 = false;
         }
     }
 
     private void FixedUpdate()
     {
         if (movePlayer1)
         {
             rb.MovePosition(v3P1);
         }
     }
 
         private void OnCollisionEnter2D(Collision2D collision)
     {
         if(collision.relativeVelocity.y > 0f)
         {         
             if (collision.gameObject.tag == "Platform")
             {
                 Vector2 velocity = rb.velocity;
                 velocity.y = bounceForce;
                 rb.velocity = velocity;
             }
         } 
     }
     
 }
 
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

74 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Move car at same speed 0 Answers

How do I make a sphere bounce forward without adding a lateral force? 1 Answer

unity bounce without physics 1 Answer

Unity 2D - Why does velocity causes the ball to change direction everytime it hits something? 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