c# - my camera wont stay on my character anymore. -
using unityengine; using system.collections; /// <summary> /// description of class# /// </summary> [requirecomponent (typeof (barseffect))] public class thirdpersoncameraz : monobehaviour { [serializefield] /// <summary> /// distance away character. /// </summary> private float distanceaway; [serializefield] /// <summary> /// distance away camera. /// </summary> private float distanceup; [serializefield] /// <summary> /// how long take photographic camera go 1 place, trying place camera. /// </summary> private float smooth; [serializefield] /// <summary> /// reason follow transform because characters position going updated using physics /// calculations. since character has stiff body on him, physics fixed update bad because /// doesnt updated often. /// </summary> private transform followxform; private float widescreen = 0.2f; private float targetingtime = 0.5f; private vector3 offset = new vector3(0f,1.5f, 0f); private vector3 lookdir; /// <summary> /// target position trying position camera. /// </summary> private vector3 targetposition; private barseffect bareffect; private camstates camstate=camstates.behind; private vector3 velocitycamsmooth= vector3.zero; private float camsmoothdamptime = 0.1f; public enum camstates { behind, firstperson, target, free } // utilize initialization void start () { ///this grab follow game object tagged player followxform = gameobject.findwithtag ("player").transform; lookdir = followxform.forward; bareffect = getcomponent<barseffect> (); if (bareffect == null) { debug.logerror("attach widescreen barseffect script camera.", this); } } // update called 1 time per frame void update () { } void ondrawgizmos() { } /// <summary> /// function making photographic camera because after position objects /// can set ur photographic camera code here , create sure lines since happens /// after update. /// </summary> void lateupdate() { // distance characters follow height. vector3 characteroffset = followxform.position + new vector3 (0f, distanceup, 0f); //determines photographic camera state //calculates direction photographic camera player, kill y , normalize give valid direction unit magnitude. // if (input.getaxis ("target") > 0.01f) { bareffect.coverage = mathf.smoothstep (bareffect.coverage, widescreen, targetingtime); camstate = camstates.target; } else { bareffect.coverage = mathf.smoothstep (bareffect.coverage, 0f, targetingtime); camstate = camstates.behind; } switch(camstate) { case camstates.behind: ///setting target position, taking follow position , moving distance, , moving /// distance away based on follow vector. /// vector.up (0,1,0) ///drawing characters position vector created. debug.drawray (followxform.position, vector3.up * distanceup, color.red); /// drawing characters position based on away distance debug.drawray(followxform.position,-1f*followxform.forward*distanceaway,color.blue); lookdir = characteroffset - this.transform.position; lookdir.y = 0; lookdir.normalize (); debug.drawray (this.transform.position, lookdir, color.green); targetposition = characteroffset + followxform.up * distanceup - lookdir * distanceaway; ///drawing line follow position target position. debug.drawray(followxform.position, targetposition,color.magenta); break; case camstates.target: lookdir=followxform.forward; break; } targetposition = characteroffset = followxform.up * distanceup - lookdir * distanceaway; compensateforwalls (characteroffset, ref targetposition); ///making smooth transition between current position , position wants in /// delta time time between frames. delta time * smooth normalizing photographic camera between frames. smoothposition(this.transform.position, targetposition); ///make sure photographic camera looking @ character/follow(gameobject) transform.lookat(followxform); } private void smoothposition (vector3 frompos, vector3 topos) { this.transform.position = vector3.smoothdamp (frompos, topos, ref velocitycamsmooth, camsmoothdamptime); } private void compensateforwalls(vector3 fromobject, ref vector3 totarget) { debug.drawline (fromobject, totarget, color.cyan); //compensate walls between cameraz raycasthit wallhit = new raycasthit (); if (physics.linecast (fromobject, totarget, out wallhit)) { debug.drawray (wallhit.point, vector3.left, color.red); totarget = new vector3 (wallhit.point.x, totarget.y, wallhit.point.z); } }
}
ok whats going on here after inputting break; case camstates.target: lookdir=followxform.forward;
break; } targetposition = characteroffset = followxform.up * distanceup - lookdir *distanceaway; my photographic camera no longer stays on character. programme made when force downwards on left trigger supposed follow character, when im not holding down, photographic camera free , character can run around camera. im confused why wont work. next unity 3rd person controller , appreciate amount of help can get! thanks!
c# unity3d
No comments:
Post a Comment