Question by
Sharkie_ · Apr 19, 2020 at 09:19 PM ·
rotationscripting problemaiming
using RayCast to aim at different planes
i'm very new to coding. I am making an isometric shooter and i wanted to be able to aim my character to different heights of terrain right now i'm using this code
public class PlayerAim : MonoBehaviour
{
void Update()
{
Plane PlayerPlane = new Plane(Vector3.up, transform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
float HitDist = 0f;
if(PlayerPlane.Raycast(ray, out HitDist))
{
Vector3 TargetPoint = ray.GetPoint(HitDist);
Quaternion TargetRotation = Quaternion.LookRotation(TargetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, TargetRotation, 7f * Time.deltaTime);
}
but this code only allows me to aim horisontaly. I tried creating game objects at different heights that would serve as "PlayerPlane" and using Physics.Raycast but i coudn't figure out how to create vector3 from that. here's code i tried for that. The error says can't use float for Vector3
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) && (hit.collider.tag == "AimPlane"))
{
Vector3 targetPoint = ray.GetPoint(hit);
}
How can i fix this, or is there a better way of doing this?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612231919im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object don't rotate correctly 1 Answer
Why won't my model rotate up on X axis? 1 Answer
Strange rotation behaviour 0 Answers
Rotate character 0 Answers
Problem with projectile rotation - 2D 0 Answers