Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 harrylongman11 · Oct 30, 2020 at 02:06 PM · addforceplayer movementaddforceatposition

Increase Rigidbody2D Velocity based on player direction

Hi everyone

So I have this script below that determines the players rotation in the direction its heading. How do i add a If mouse down function to addForce to its current trajectory?

Any help would be greatly appreciated!!!

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerVelocity : MonoBehaviour {

  Vector3 startPos, endPos, direction;
  public Rigidbody2D myRigidbody;

  public float thrust = 10.0f;

  

 
 

  void Start()
  {
      myRigidbody = GetComponent<Rigidbody2D> ();
      transform.position = new Vector3(0.0f, -2.0f, 0.0f);

  }

 

 void Update () {

     
      Vector2 moveDirection = myRigidbody.velocity;
      if (moveDirection != Vector2.zero) {
          float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);      
       }

       
       

 }

}

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

1 Reply

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

Answer by Overature · Oct 30, 2020 at 04:05 PM

Create an if statement in the update block that detects when the mouse button is down, then modify the position of the object you want to move. Something like this:

 if(Input.GetMouseButtonDown(0)){
  float distance = 5f;
  transform.position = new Vector3(transform.postion.x + transform.position.y + distance, 
          transform.positon.z)
 } 
 

This will move the object by 5 in the y direction every frame that the mouse button is held down. Change the distance value to make it faster or slower or negative if you wish to change direction. You can also add change x and z position in the same manor. I hope that helps.

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 harrylongman11 · Nov 02, 2020 at 02:09 PM 0
Share

I actually managed to create the affect i was after!

I just increased my rigidBodys velocity.

see below :)

Vector3 startPos, endPos, direction; public Rigidbody2D myRigidbody;

  public float force = 0f;
  public float pull = 0f;

  

 
 

  void Start()
  {
      myRigidbody = GetComponent<Rigidbody2D> ();
      transform.position = new Vector3(0.0f, -2.0f, 0.0f);

  }

 

 public void Update () 
 {


      Vector2 moveDirection = myRigidbody.velocity;
      if (moveDirection != Vector2.zero) {
          float angle = $$anonymous$$athf.Atan2(moveDirection.y, moveDirection.x) * $$anonymous$$athf.Rad2Deg;
          transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);      
       }

       if (Input.Get$$anonymous$$ouseButtonDown (0)) 
      
      {
           myRigidbody.velocity /= force;
           Debug.Log("forceApplied");
           myRigidbody.mass += pull;
           Debug.Log("PullApplied");
          
      }

      if (Input.Get$$anonymous$$ouseButtonUp (0)) 
      
      {
           
           myRigidbody.velocity *= force;
           Debug.Log("forceReduced");
           myRigidbody.mass -= pull;
           Debug.Log("PullApplied");
           
      }

     

 }

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

143 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 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

Controllable ball is ricocheting off mesh collider at high speeds. 1 Answer

Unity 2019.3.0f6 - Trouble with physics, cubes turn left after some meters 1 Answer

AddForce to position on Rigidbody 2 Answers

Start Button 2 Answers

How to apply a force after launching the player? 0 Answers


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