- Home /
My rotate dont work, using raycast, C#
Hello fellows I dont know why, but my object dont rotate after the mouse.
this is my script
private void Update()
{
// raycast for turning the object against the mouse
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
transform.LookAt(hit.point);
}
// function for moving forward, against the mouse
if (Input.GetKey(KeyCode.W))
{
float MoveFwd = Input.GetAxis("Vertical") * Speed * Time.deltaTime;
transform.Translate(0, 0, MoveFwd);
}
// function for strafing side to side
float MoveSides = Input.GetAxis("Horizontal") * StrafeSpeed * Time.deltaTime;
transform.Translate(MoveSides, 0, 0);
transform.rotation = Quaternion.Euler(transform.rotation.x, 0.0f, transform.rotation.z);
transform.position = new Vector3(transform.position.x, 0.3f, transform.position.z);
}
i have a collider under my char, so thats how the ray is called, but my object wont rotate :S it follows the mouse, but dosnt turn, and thats pretty ignoring when my character is a ship, turning the same way all the time (: please help a probled new programmer
Answer by turtlelizer · Jan 21, 2012 at 04:11 PM
// Dunno why, if i remove it, the ship wont turn
public GameObject mySHIP;
transform.position = new Vector3(transform.position.x, 0.3f, transform.position.z); transform.rotation = Quaternion.Euler(mySHIP.transform.rotation.x, mySHIP.transform.rotation.y, 0.0f);
This is actual the script there get it to work, dunno why, have no referance to the GameObject in unity.... but without it wont work