Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

How to add force to a raycast hit object. C#

$
0
0
Hey guys just wondering as the title says. I am trying to make a mario galaxy kind of mechanic where I am attracted towards a planet that I raycast. I am stuck, if you can give me a little bit of help here I would appreaceate it! GameObject attractedTo; public float strengthOfAttraction = 10.0f; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast (ray, out hit)){ Debug.Log(hit.point); attractedTo = new Vector3(hit); Vector3 direction = attractedTo.transform.position - transform.position; Vector3 planetDirection = strengthOfAttraction * direction; rigidbody.AddForce((planetDirection - transform.position).normalized); }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>