Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 broooklyn8834 · 6 days ago · playercontrolsdash

i wanna make my charactor dash to the mouse direction,how to dash to the mouse direction

i wanna make my charactor dash to direction of my mouse how should i do it here is my code please help me:)

using System.Collections; using UnityEngine;

public class PlayerGrappling : MonoBehaviour { private Rigidbody2D _rigidbody2D; private LineRenderer _lineRenderer; private Transform _closest; private SpringJoint2D _springJoint2D; private Collider2D collision; private SpriteRenderer spriteRenderer;

 private float _maxSpeed = 30f;

 public GameObject hitEffect;
 public GameObject dieEffect;
 public GameObject player;
 public GameObject Diepoint;
 public CircleCollider2D CCollider;

 public float forceAmount;
 private void Start()
 {
     _rigidbody2D = GetComponent<Rigidbody2D>();
     _lineRenderer = GetComponent<LineRenderer>();
     collision = GetComponent<Collider2D>();
     spriteRenderer = GetComponent<SpriteRenderer>();
     _lineRenderer.positionCount = 0;
 }

 private void Update()
 {
     if (Input.GetMouseButtonDown(1))
     {
     dash();
     }

     if (Input.GetMouseButton(0))
     {
         // draw line from player to hinge
         _lineRenderer.SetPosition(0, transform.position);
         _lineRenderer.SetPosition(1, _closest.position);
         _rigidbody2D.AddForce(transform.forward * forceAmount, ForceMode2D.Impulse);

         if (_springJoint2D.distance > 3)
         {
             _springJoint2D.distance -= 0.01f;
         }
     }

     if (Input.GetMouseButtonUp(0))
     {
         // remove line and hinge
         _lineRenderer.positionCount = 0;
         _springJoint2D.connectedBody = null;
     }
 }


 private void FixedUpdate()
 {

     if (_rigidbody2D.velocity.magnitude > _maxSpeed)
     {
         _rigidbody2D.velocity = _rigidbody2D.velocity.normalized * _maxSpeed;
     }
 }

 Transform GetClosestHinge(GameObject[] hinges)
 {
     Transform closest = null;
     var closestDistanceSqr = Mathf.Infinity;
     var currentPosition = transform.position;
     foreach (var potentialTarget in hinges)
     {
         var directionToTarget = potentialTarget.transform.position - currentPosition;
         var dSqrToTarget = directionToTarget.sqrMagnitude;
         if (!(dSqrToTarget < closestDistanceSqr)) continue;
         closestDistanceSqr = dSqrToTarget;
         closest = potentialTarget.transform;
         CCollider.enabled = true;
     }

     return closest;
 }

 void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "unable")
     {
         blue();

     }

     if (collision.gameObject.tag == "Die") 
     {
         Die();
         _lineRenderer.positionCount = 0;
         _springJoint2D.connectedBody = null;
     }


 }
 void Die()
 {
       Debug.Log("Die");
         Destroy(player);
     Instantiate(dieEffect, collision.transform.position, Quaternion.identity);
 }
 void blue()
 {
         spriteRenderer.color = new Color(1, 1, 1, 0.4f);
         _springJoint2D.enabled = false;
 }
 void dash() {


  } 

}

,i wanna add the dash abillity to my player my player charactor is unable to move because i didnt code it to so i just wanna make it dash to the mouse pointer's direction my code is this

using System.Collections; using UnityEngine;

public class PlayerGrappling : MonoBehaviour { private Rigidbody2D _rigidbody2D; private LineRenderer _lineRenderer; private Transform _closest; private SpringJoint2D _springJoint2D; private Collider2D collision; private SpriteRenderer spriteRenderer;

 private float _maxSpeed = 30f;

 public GameObject hitEffect;
 public GameObject dieEffect;
 public GameObject player;
 public GameObject Diepoint;
 public CircleCollider2D CCollider;

 public float forceAmount;
 private void Start()
 {
     _rigidbody2D = GetComponent<Rigidbody2D>();
     _lineRenderer = GetComponent<LineRenderer>();
     collision = GetComponent<Collider2D>();
     spriteRenderer = GetComponent<SpriteRenderer>();
     _lineRenderer.positionCount = 0;
 }

 private void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         // get the closest hinge
         _closest = GetClosestHinge(GameObject.FindGameObjectsWithTag("hinge"));
         // attach joint to it
         _springJoint2D = _closest.GetComponent<SpringJoint2D>();
         _springJoint2D.connectedBody = _rigidbody2D;
         // set line renderer position count
         _lineRenderer.positionCount = 2;
     }

     if (Input.GetMouseButton(0))
     {
         // draw line from player to hinge
         _lineRenderer.SetPosition(0, transform.position);
         _lineRenderer.SetPosition(1, _closest.position);
         _rigidbody2D.AddForce(transform.forward * forceAmount, ForceMode2D.Impulse);

         if (_springJoint2D.distance > 3)
         {
             _springJoint2D.distance -= 0.01f;
         }
     }

     if (Input.GetMouseButtonUp(0))
     {
         // remove line and hinge
         _lineRenderer.positionCount = 0;
         _springJoint2D.connectedBody = null;
     }
 }


 private void FixedUpdate()
 {

     if (_rigidbody2D.velocity.magnitude > _maxSpeed)
     {
         _rigidbody2D.velocity = _rigidbody2D.velocity.normalized * _maxSpeed;
     }
 }

 Transform GetClosestHinge(GameObject[] hinges)
 {
     Transform closest = null;
     var closestDistanceSqr = Mathf.Infinity;
     var currentPosition = transform.position;
     foreach (var potentialTarget in hinges)
     {
         var directionToTarget = potentialTarget.transform.position - currentPosition;
         var dSqrToTarget = directionToTarget.sqrMagnitude;
         if (!(dSqrToTarget < closestDistanceSqr)) continue;
         closestDistanceSqr = dSqrToTarget;
         closest = potentialTarget.transform;
         CCollider.enabled = true;
     }

     return closest;
 }

 void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "unable")
     {
         blue();

     }

     if (collision.gameObject.tag == "Die") 
     {
         Die();
         _lineRenderer.positionCount = 0;
         _springJoint2D.connectedBody = null;
     }


 }
 void Die()
 {
       Debug.Log("Die");
         Destroy(player);
     Instantiate(dieEffect, collision.transform.position, Quaternion.identity);
 }
 void blue()
 {
         spriteRenderer.color = new Color(1, 1, 1, 0.4f);
         _springJoint2D.enabled = false;
 }

}

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
0

Answer by TheTrueDuck · 3 days ago

Hi! The simplest way to do something like this would be to just get the positions of both the player and mouse, and use some basic vector math to get the direction out of those. That might look a little like this:

 public void Dash()
 {
     float dashSpeed = 10f;
 
     Vector3 playerScreenPosition = Camera.main.WorldToScreenPoint(player.transform.position);
     Vector3 mouseScreenPosition = Input.mousePosition;
 
     Vector3 playerToMouseVector = (mouseScreenPosition - playerScreenPosition).normalized;
     
     _rigidbody2D.velocity = playerToMouseVector * dashSpeed;
 }

The above snippet normalizes the playerToMouseVector to make the player always dash with the same speed, regardless of where you click. (if clicking further away should make you dash further, just remove the '.normalized' part.)

You will probably also want to mess around with the friction on the RigidBody to control how quickly the player should slow down and stop, as well as the dashSpeed in the above function.

Hope this helps :)

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

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

Jumping not allways registering fix? 0 Answers

how make this script follow a target. 1 Answer

Camera rotates around player but player movement is incorrect 1 Answer

Dash in movement direction, not in forward direction 0 Answers

Player or character select 2 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