- Home /
use the raycast with the rotation
I am creating a script to move an object along the surface of a sphere. To do this, create an object not rendered shooting a ray cast and places the player in accordance with section with whom he collided. The problem is that when my object wheel (around the ball) does not throw more than the radius because I gave such direction Vector3.down. How can I fix?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public Transform targetCenter;
public Transform worldCenter;
public float radius;
public float time;
// float distance;
void Start()
{
//pos = this.transform.position;
}
void Update()
{
RaycastHit hit;
//posizione
var distance = (targetCenter.transform.position-worldCenter.transform.position).normalized;
var rot = targetCenter.transform.rotation.eulerAngles;
rot.z = Mathf.Atan2(distance.y, distance.x) * Mathf.Rad2Deg;
targetCenter.transform.rotation = Quaternion.Euler(rot);
Ray ray = new Ray(targetCenter.transform.position, -targetCenter.transform.up);
if (Input.GetKey(KeyCode.RightArrow))
{
targetCenter.transform.position = new Vector3(-Mathf.Cos(Time.time) * radius, Mathf.Sin(Time.time) * radius, 0f);
}
if (Physics.Raycast(targetCenter.transform.position,Vector3.down,out hit, Mathf.Infinity))
{
Debug.DrawLine(targetCenter.transform.position, hit.point, Color.blue);
this.transform.position = new Vector3(hit.point.x, hit.point.y-30.0f, 0f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
rotation problems when snapping to terrain 1 Answer
Vertical auto-aim with raycasts 1 Answer
Hover Effect with more rays as turbines? 1 Answer
Raycast stop at distance 3 Answers
RaycastHit Collider Question 1 Answer