Local vs World Raycast

Im trying to setup that elusive third person camera.

var Camera: Camera;
private var hit: RaycastHit;
function Update () {

	
	 if (Physics.Raycast (transform.localPosition, -Vector3.forward, hit)) {
        distanceToWall = hit.distance;
        Debug.Log(distanceToWall);
	 }
	
  			var forward = transform.TransformDirection(-Vector3.forward) * distanceToWall;
  			Debug.DrawRay (transform.position, forward * 10, Color.green); 
 
	Camera.transform.position=hit.transform.position;


}

A few things with this test setup:

So In this code I Successfuly Debug Drawline Backwards, but 1. Id like the ray to stop when it collides with something.

  1. A Debug Drawline is one thing but the actual raycast only works in world space. How do I get the raycast to align with the main objects local axis?

  2. Once I get a hitpoint, what sort of code would I use to offset the camera from the “wall”? Do I need to find the walls normal and transform.translate(0,0,someFactor)?

Thanks for any advice
AaronC

86035–3333–$collisiontest_192.zip (9.2 KB)

If you have a vector in camera space and need it to be in wrold space, just use worldVector=transform.rotation*localVector;

when you get a hit in your “hit” variable, hit.normal will always point straight out from the wall and be 1 unit in length. So newPosition = hit.point+hit.normal*distanceFromWall; will give you a proper position.

Also: var Camera : Camera; will cause bad things to happen because your variable is the same name as the class. Name it something else, like myCamera.

Thanks Ray

I dont think we’re talking about the same thing, I have a rotating sphere(Courtesy of MouseLook) and although its rotating, its only casting a ray in one constant direction. So I want to raycast from a local transform. So when I rotate the sphere, the cast ray rotates too. Thats not happening for me at this point. Or am I just not understanding your suggestion?

EDIT
@7 Hours Later-Nevermind, I adapted some Raycast code from the machinegun.js script-woooo!

Perfect

Uh okay, Duly noted. Thanks, I’ll keep an eye out not to do that.

So at this point, Im almost there. Only one thing before the boys stress test this in game, and thats getting Lerp to work:

var MyCamera: Camera;
var distanceFromWall: float=0.1;

var otherScript: SmoothFollow;
//When No RaycastHit, use the smoothfollow on the Camera ;-)
var DummyLocatorNode: Transform;
//Keep a Reference position for the Lerp-This has Smooth Follow on it too

private var hit: RaycastHit;

function Update () {
	
	var direction = transform.TransformDirection(-Vector3.forward);

	
	 	if (Physics.Raycast (transform.position, direction, hit)) {
	 		otherScript.enabled=false;
	 		//Smoothfollow Disabled
        distanceToWall = hit.distance;
        newPosition = hit.point+hit.normal*distanceFromWall;
        //Thats where we want the Camera to be:
    
  	MyCamera.transform.position = Vector3.Lerp(MyCamera.transform.position, newPosition, Time.time);
  	//That line doesnt lerp though-I thought It would? 
 
        Debug.Log("RaycastHit");
       // return; Do I use a return here?
	 }
	 else
	 {
	 		MyCamera.transform.position = Vector3.Lerp(MyCamera.transform.position, DummyLocatorNode.position, Time.time);
	 		//Hey that Lerp doesnt work either-whats goin' on then ay?
	 	otherScript.enabled=true;
	 	//Yep turn that smooth follow on, AFTER the Lerp already!
	 		 	
	 }
}

What do you think? Is there something inherently different about declaring a varible as a Transform then accessing its position, and declaring something as a GameObject, and accessing its transform.position? Thanks Heaps Ray

AaronC

86231–3351–$collisiontest_2_347.zip (11.9 KB)

You have Time.time as the third variable in the Lerp. You should be using Time.deltaTime.

Excellent. Thanks!

So Time.deltaTime is a fixed timestep, yet Time.time is…different? The time since the frame started? Thats Hell confusing…What does that mean?

This is coming together, thanks guys.
AaronC

Time.time is the total time the game has been running. So a minute into the game it’s 60.0. Delta time is always the time per frame (so 50fps is 0.02).